Seamly2D
Code documentation
show_info_dialog.cpp
Go to the documentation of this file.
1 /***********************************************************************
2  **
3  ** @file show_info_dialog.cpp
4  ** @author DSCaskey <dscaskey@gmail.com>
5  ** @date Nov 6, 2022
6  **
7  ** @brief
8  ** @copyright
9  ** This source code is part of the Valentine project, a pattern making
10  ** program, whose allow create and modeling patterns of clothing.
11  ** Copyright (C) 2013-2015 Seamly2D project
12  ** All Rights Reserved.
13  **
14  ** Seamly2D is free software: you can redistribute it and/or modify
15  ** it under the terms of the GNU General Public License as published by
16  ** the Free Software Foundation, either version 3 of the License, or
17  ** (at your option) any later version.
18  **
19  ** Seamly2D is distributed in the hope that it will be useful,
20  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  ** GNU General Public License for more details.
23  **
24  ** You should have received a copy of the GNU General Public License
25  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "show_info_dialog.h"
30 #include "ui_show_info_dialog.h"
31 
32 #include <QApplication>
33 #include <QClipboard>
34 #include <QFileDialog>
35 #include <QFont>
36 #include <QImage>
37 #include <QPageLayout>
38 #include <QPixmap>
39 #include <QPrinter>
40 #include <QPrintDialog>
41 #include <QPrintPreviewDialog>
42 #include <QShowEvent>
43 #include <QString>
44 #include <QTextDocument>
45 #include <QtWidgets>
46 
47 #include "../core/vapplication.h"
48 #include "../xml/vpattern.h"
49 
50 //---------------------------------------------------------------------------------------------------------------------
51 /*
52  * @brief Constructor.
53  *
54  * Displays the xml pattern information in a dialog. Provides functionality to copy the
55  * context to the clipboard, send it to the printer, or save as a PDF file.
56  *
57  * @param parent Parent object of the dialog.
58  */
60  : QDialog(parent)
61  , ui(new Ui::ShowInfoDialog)
62  , doc(doc)
63  , m_isInitialized(false)
64 {
65  ui->setupUi(this);
66  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
67 
68  qApp->Seamly2DSettings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
69 
70  QByteArray byteArray;
71  byteArray.append(doc->GetImage().toUtf8());
72  QString url = QString("<img src=\"data:image/png;base64, ") + byteArray + "\" width = 280 />";
73 
74  const QString info = tr("<table style=font-size:11pt; font-weight:600>"
75  "<tr><td align = right><b>Company: </b></td><td>%1</td><tr>"
76  "<tr><td align = right><b>Customer: </b></td><td>%2</td><tr>"
77  "<tr><td align = right><b>Pattern Name:</b></td><td>%3</td><tr>"
78  "<tr><td align = right><b>Pattern No: </b></td><td>%4</td><tr>"
79  "<tr><td align = right><b>Version: </b></td><td>%5</td><tr>"
80  "<tr><td align = right><b>Units: </b></td><td>%6</td><tr>"
81  "<tr><td align = right><b>Measurements:</b></td><td>%7<br></td><tr>"
82  "<tr><td align = right><b>Description: </b></td><td>%8<br></td><tr>"
83  "<tr><td align = right><b>Notes: </b></td><td>%9<br></td><tr>"
84  "<tr><td align = right><b>Image: </b></td><td>%10</td><tr>"
85  "</table>")
86  .arg(doc->GetCompanyName()) //1
87  .arg(doc->GetCustomerName()) //2
88  .arg(doc->GetPatternName()) //3
89  .arg(doc->GetPatternNumber()) //4
90  .arg(doc->GetVersion()) //5
91  .arg(UnitsToStr(doc->MUnit())) //6
92  .arg(doc->MPath()) //7
93  .arg(doc->GetDescription()) //8
94  .arg(doc->GetNotes()) //9
95  .arg(url); //10
96 
97  ui->info_TextBrowser->setHtml(info);
98 
99  connect(ui->clipboard_ToolButton, &QToolButton::clicked, this, &ShowInfoDialog::copyToClipboard);
100  connect(ui->printer_ToolButton, &QToolButton::clicked, this, &ShowInfoDialog::sendToPrinter);
101  connect(ui->pdf_ToolButton, &QToolButton::clicked, this, &ShowInfoDialog::exportPdf);
102 }
103 
104 //---------------------------------------------------------------------------------------------------------------------
105 /*
106  * Destructor
107  *
108 */
110 {
111  delete ui;
112 }
113 
114 //---------------------------------------------------------------------------------------------------------------------
115 /*
116  * @brief Reimplement QWidget showEvent.
117  *
118 */
119 //---------------------------------------------------------------------------------------------------------------------
120 void ShowInfoDialog::showEvent(QShowEvent *event)
121 {
122  QDialog::showEvent(event);
123  if (event->spontaneous())
124  {
125  return;
126  }
127 
128  if (m_isInitialized)
129  {
130  return;
131  }
132 
133  setMaximumSize(size());
134  setMinimumSize(size());
135 
136  m_isInitialized = true;//first show windows are held
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
140 /*
141  * @brief Copies the text content of the text browser widget to the clipboard.
142  */
144 {
145  QClipboard *clipboard = QApplication::clipboard();
146  clipboard->setText(ui->info_TextBrowser->toPlainText());
147 
148  ui->info_TextBrowser->selectAll();
149  ui->info_TextBrowser->copy();
150 }
151 
152 //---------------------------------------------------------------------------------------------------------------------
153 /*
154  * @brief Sends content of the text browser widget to the printer.
155  */
157 {
158  QPrinter printer;
159  QPrintDialog printDialog(&printer);
160  if(printDialog.exec())
161  {
162  QTextDocument textDocument;
163  textDocument.setHtml(ui->info_TextBrowser->toHtml());
164  textDocument.print(&printer);
165  }
166 }
167 
168 //---------------------------------------------------------------------------------------------------------------------
169 /*
170  * @brief Exports the content of the text browser widget as a pdf.
171  */
173 {
174  QString filters(tr("Info files") + QLatin1String("(*.pdf)"));
175  QString filename = QFileDialog::getSaveFileName(this, tr("Export PDF"),
176  doc->GetPatternName() + tr("_info") + QLatin1String(".pdf"),
177  filters, nullptr, QFileDialog::DontUseNativeDialog);
178 
179  if (QFileInfo(filename).suffix().isEmpty())
180  {
181  filename.append(".pdf");
182  }
183  QPrinter printer(QPrinter::PrinterResolution);
184  printer.setOutputFormat(QPrinter::PdfFormat);
185  printer.setPageSize(printer.pageLayout().pageSize());
186  printer.setOutputFileName(filename);
187 
188  QTextDocument textDocument;
189  textDocument.setHtml(ui->info_TextBrowser->toHtml());
190  textDocument.setPageSize(printer.pageLayout().paintRectPixels(printer.resolution()).size());
191  textDocument.print(&printer);
192 }
Ui::ShowInfoDialog * ui
ShowInfoDialog(VPattern *doc, QWidget *parent=nullptr)
virtual void showEvent(QShowEvent *event) Q_DECL_OVERRIDE
virtual ~ShowInfoDialog()
QString GetCompanyName() const
QString MPath() const
QString GetPatternName() const
QString GetPatternNumber() const
QString GetVersion() const
QString GetDescription() const
QString GetImage() const
QString GetCustomerName() const
QString GetNotes() const
Unit MUnit() const
The VPattern class working with pattern file.
Definition: vpattern.h:68
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define qApp
Definition: vapplication.h:67