Debian Codegraph @

src/album/widgets/cexportimagedialog.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "cexportimagedialog.h"
22 #include "mainwindow.h"
23
24 #include <DFileDialog>
25 #include <DDialog>
26 #include <DMessageBox>
27 #include <DTitlebar>
28
29 #include <QFormLayout>
30 #include <QImageWriter>
31 #include <QStandardPaths>
32 #include <QPdfWriter>
33 #include <QPainter>
34 #include <QDesktopWidget>
35
36 #include <QDebug>
37 #include <QDateTime>
38 #include <QTimer>
39
40
41 const QSize DIALOG_SIZE = QSize(380, 280);
42 const QSize LINE_EDIT_SIZE = QSize(250, 35);
43
44 CExportImageDialog::CExportImageDialog(DWidget *parent)
45 : DDialog(parent), m_fileNameEdit(nullptr), m_savePathCombox(nullptr)
46 , m_formatCombox(nullptr), m_qualitySlider(nullptr), m_qualityLabel(nullptr)
47 , m_quality(0), m_questionDialog(nullptr), m_emptyWarningDialog(nullptr)
48 {
49 initUI();
50 initConnection();
51 }
52
53 CExportImageDialog::~CExportImageDialog()
54 {
55
56 }
57
58 void CExportImageDialog::showMe(const QPixmap &pixmap)
59 {
60 m_saveImage = pixmap;
61
62 if (m_savePathCombox->count() == Other + 1) {
63 m_savePathCombox->blockSignals(true);
64 m_savePathCombox->removeItem(Other);
65 }
66 m_savePathCombox->blockSignals(false);
67
68 m_savePathCombox->setCurrentIndex(Pictures);
69 m_formatCombox->setCurrentIndex(JPG);
70 m_qualitySlider->setValue(100);
71
72
73 slotOnSavePathChange(Pictures);
74 slotOnFormatChange(JPG);
75 slotOnQualityChanged(m_qualitySlider->value());
76
77 show();
78 }
79
80 void CExportImageDialog::setPicFileName(QString strFileName)
81 {
82 QString name = strFileName.mid(0, strFileName.lastIndexOf("."));
83 m_fileNameEdit->setText(name);
84 }
85
86 void CExportImageDialog::setGifType(const QString &strFilePath)
87 {
88 gifpath = strFilePath;
89 m_formatCombox->addItem(tr("gif"));
90 m_formatCombox->setCurrentText(tr("gif"));
91 }
92
93 void CExportImageDialog::removeGifType()
94 {
95 gifpath = "";
96 for (int i = 0; i < m_formatCombox->count(); i++) {
97 if (tr("gif") == m_formatCombox->itemText(i)) {
98 m_formatCombox->removeItem(i);
99 return;
100 }
101 }
102 }
103
104 void CExportImageDialog::showEvent(QShowEvent *evet)
105 {
106 // 初始编辑框焦点设置
107 m_fileNameEdit->lineEdit()->selectAll();
108 m_fileNameEdit->lineEdit()->setFocus(); //设置焦点
109 return QWidget::showEvent(evet);
110 }
111
112 void CExportImageDialog::keyPressEvent(QKeyEvent *e)
113 {
114 // combox空间enter展开选项
115 if (e->key() == Qt::Key_Return) {
116 if (m_savePathCombox->hasFocus()) {
117 m_savePathCombox->showPopup();
118 } else if (m_formatCombox->hasFocus()) {
119 m_formatCombox->showPopup();
120 }
121 e->accept();
122 } else if (e->key() == Qt::Key_Escape) {
123 this->close();
124 }
125 QWidget::keyPressEvent(e);
126 }
127
128 void CExportImageDialog::initUI()
129 {
130 setFixedSize(DIALOG_SIZE);
131 setModal(true);
132
133 setContentsMargins(0, 0, 0, 0);
134
135 DLabel *logoLable = new DLabel(this);
136 QIcon icon = QIcon::fromTheme("deepin-album");
137 logoLable->setPixmap(icon.pixmap(QSize(30, 30)));
138
139 logoLable->move(12, 7);
140 logoLable->setAlignment(Qt::AlignLeft);
141
142 setWindowTitle(tr("Export"));
143
144 m_fileNameEdit = new DLineEdit(this);
145 //m_fileNameEdit->setFixedSize(LINE_EDIT_SIZE);
146 m_fileNameEdit->setClearButtonEnabled(false);
147 // m_fileNameEdit->setFocusPolicy(Qt::ClickFocus);
148
149 connect(m_fileNameEdit, &DLineEdit::editingFinished, this, [ = ] {
150 QString arg = m_fileNameEdit->text();
151 int len = arg.toLocal8Bit().size();
152 int filemaxlen = 251;
153 if (m_saveFormat.toUpper().compare("JPEG") == 0)
154 filemaxlen -= 1;
155 QString Interceptstr;
156 if (len > filemaxlen)
157 {
158 int num = 0;
159 int pos = 0;
160 for (; pos < arg.size(); pos++) {
161 if (arg.at(pos) >= 0x4e00 && arg.at(pos) <= 0x9fa5) {
162 num += 3;
163 if (num >= filemaxlen) {
164 break;
165 }
166 } else if (num < filemaxlen) {
167 num += 1;
168 } else {
169 break;
170 }
171 }
172 Interceptstr = arg.left(pos);
173 m_fileNameEdit->setText(Interceptstr);
174 }
175 });
176
177 m_savePathCombox = new DComboBox(this);
178 m_savePathCombox->setFocusPolicy(Qt::TabFocus);
179 m_savePathCombox->insertItem(Pictures, tr("Pictures"));
180 m_savePathCombox->insertItem(Documents, tr("Documents"));
181 m_savePathCombox->insertItem(Downloads, tr("Downloads"));
182 m_savePathCombox->insertItem(Desktop, tr("Desktop"));
183 m_savePathCombox->insertItem(Videos, tr("Videos"));
184 m_savePathCombox->insertItem(Music, tr("Music"));
185 m_savePathCombox->insertItem(UsrSelect, tr("Select other directories"));
186
187 //m_savePathCombox->setFixedSize(LINE_EDIT_SIZE);
188
189
190 m_formatCombox = new DComboBox(this);
191 m_formatCombox->setFocusPolicy(Qt::TabFocus);
192 // m_formatCombox->insertItem(JPG, "jpg");
193 // m_formatCombox->insertItem(JPG, "jpeg");
194 // m_formatCombox->insertItem(PNG, "png");
195 // m_formatCombox->insertItem(BMP, "bmp");
196 // m_formatCombox->insertItem(BMP, "pgm");
197 // m_formatCombox->insertItem(TIF, "xbm");
198 // m_formatCombox->insertItem(PDF, "xpm");
199 m_formatCombox->insertItem(JPG, "jpg");
200 m_formatCombox->insertItem(JPEG, "jpeg");
201 m_formatCombox->insertItem(PNG, "png");
202 m_formatCombox->insertItem(BMP, "bmp");
203 m_formatCombox->insertItem(PGM, "pgm");
204 m_formatCombox->insertItem(XBM, "xbm");
205 m_formatCombox->insertItem(XPM, "xpm");
206 //m_formatCombox->setFixedSize(LINE_EDIT_SIZE);
207
208 m_qualitySlider = new DSlider(Qt::Horizontal, this);
209 m_qualitySlider->slider()->setFocusPolicy(Qt::TabFocus);
210 m_qualitySlider->setMinimum(1);
211 m_qualitySlider->setMaximum(100);
212 m_qualitySlider->setValue(100);
213 //m_qualitySlider->setFixedSize(QSize(100, LINE_EDIT_SIZE.height()));
214
215 m_qualityLabel = new DLabel(this);
216 //m_qualityLabel->setFixedHeight(LINE_EDIT_SIZE.height());
217
218 QHBoxLayout *qualityHLayout = new QHBoxLayout;
219 qualityHLayout->setMargin(0);
220 qualityHLayout->setSpacing(0);
221 qualityHLayout->addSpacing(3);
222 qualityHLayout->addWidget(m_qualitySlider);
223 qualityHLayout->addSpacing(20);
224 qualityHLayout->addWidget(m_qualityLabel);
225
226 DWidget *contentWidget = new DWidget(this);
227 contentWidget->setContentsMargins(0, 0, 0, 0);
228 QFormLayout *fLayout = new QFormLayout(contentWidget);
229 fLayout->setFormAlignment(Qt::AlignJustify);
230 fLayout->setHorizontalSpacing(10);
231 fLayout->addRow(tr("Name:"), m_fileNameEdit);
232 fLayout->addRow(tr("Save to:"), m_savePathCombox);
233 fLayout->addRow(tr("Format:"), m_formatCombox);
234 fLayout->addRow(tr("Quality:"), qualityHLayout);
235
236 addContent(contentWidget);
237
238 addButton(tr("Cancel"), false, DDialog::ButtonNormal);
239 addButton(tr("Save"), true, DDialog::ButtonRecommend);
240
241 m_emptyWarningDialog = new DDialog(this);
242 m_emptyWarningDialog->setModal(true);
243 m_emptyWarningDialog->addButtons(QStringList() << tr("OK"));
244 m_emptyWarningDialog->setFixedSize(400, 170);
245
246 //剔除Titlebar焦点
247 DTitlebar *titlebar = findChild<DTitlebar *>();
248 if (titlebar) {
249 titlebar->setFocusPolicy(Qt::ClickFocus);
250 }
251
252 QWidget *closeButton = findChild<QWidget *>("DTitlebarDWindowCloseButton");
253 if (closeButton) {
254 closeButton->setFocusPolicy(Qt::TabFocus);
255 }
256 }
257
258 void CExportImageDialog::initConnection()
259 {
260 connect(m_savePathCombox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotOnSavePathChange(int)));
261 connect(m_formatCombox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotOnFormatChange(int)));
262 connect(this, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(slotOnDialogButtonClick(int, const QString &)));
263 connect(m_qualitySlider, SIGNAL(valueChanged(int)), this, SLOT(slotOnQualityChanged(int)));
264 connect(m_emptyWarningDialog, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(slotOnEmptyWarningDialogButtonClick(int, const QString &)));
265 }
266
267 void CExportImageDialog::slotOnSavePathChange(int index)
268 {
269 switch (index) {
270 case Pictures:
271 m_savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
272 break;
273 case Documents:
274 m_savePath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
275 break;
276 case Downloads:
277 m_savePath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
278 break;
279 case Desktop:
280 m_savePath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
281 break;
282 case Videos:
283 m_savePath = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
284 break;
285 case Music:
286 m_savePath = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
287 break;
288 case UsrSelect:
289 showDirChoseDialog();
290 break;
291 case Other:
292 m_savePath = m_savePathCombox->itemText(index);
293 break;
294 default:
295 m_savePath = "";
296 break;
297 }
298 }
299
300 void CExportImageDialog::slotOnFormatChange(int index)
301 {
302 m_saveFormat = m_formatCombox->itemText(index);
303 //when jpeg format,recalculate file name length ,because ".jpeg"==5
304 if (m_saveFormat.toUpper().compare("JPEG") == 0) {
305 QString arg = m_fileNameEdit->text();
306 int len = arg.toLocal8Bit().size();
307 QString Interceptstr;
308 if (len > 250) {
309 unsigned num = 0;
310 int pos = 0;
311 for (; pos < arg.size(); pos++) {
312 if (arg.at(pos) >= 0x4e00 && arg.at(pos) <= 0x9fa5) {
313 num += 3;
314 if (num >= 250) {
315 break;
316 }
317 } else if (num < 250) {
318 num += 1;
319 } else {
320 break;
321 }
322 }
323 Interceptstr = arg.left(pos);
324 m_fileNameEdit->setText(Interceptstr);
325 }
326 }
327 }
328
329 void CExportImageDialog::slotOnDialogButtonClick(int index, const QString &text)
330 {
331 Q_UNUSED(text)
332
333 if (index == 1) {
334 QString filename = m_fileNameEdit->text().trimmed();
335 if ("" == filename) {
336 hide();
337 showEmptyWarningDialog();
338 return;
339 }
340 QString completePath = m_savePath + "/" + filename.trimmed() + "." + m_saveFormat;
341 QFileInfo fileinfo(completePath);
342 if (fileinfo.exists()) {
343 if (!fileinfo.isDir()) {
344 hide();
345 showQuestionDialog(completePath);
346 return;
347 }
348 }
349 bool savere = doSave();
350 hide();
351 if (savere) {
352 emit dApp->signalM->ImgExportSuccess();
353 } else {
354 emit dApp->signalM->ImgExportFailed();
355
356 }
357
358 }
359 }
360
361 void CExportImageDialog::slotOnEmptyWarningDialogButtonClick(int index, const QString &text)
362 {
363 Q_UNUSED(text);
364 Q_UNUSED(index);
365 m_emptyWarningDialog->hide();
366 }
367
368 void CExportImageDialog::slotOnQuestionDialogButtonClick(int index, const QString &text)
369 {
370 Q_UNUSED(text);
371 if (index == 1) {
372 if (doSave()) {
373 emit dApp->signalM->ImgExportSuccess();
374 } else {
375 emit dApp->signalM->ImgExportFailed();
376 }
377 }
378 }
379
380 void CExportImageDialog::slotOnQualityChanged(int value)
381 {
382 m_qualityLabel->setText(QString("%1%").arg(value));
383 m_quality = value;
384 }
385
386 void CExportImageDialog::showDirChoseDialog()
387 {
388 DFileDialog dialog(this);
389 dialog.setDirectory(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
390 dialog.setViewMode(DFileDialog::Detail);
391 dialog.setFileMode(DFileDialog::DirectoryOnly);
392 if (dialog.exec()) {
393 QString fileDir = dialog.selectedFiles().first();
394 if (m_savePathCombox->count() < Other + 1) {
395 m_savePathCombox->insertItem(Other, fileDir);
396 } else {
397 m_savePathCombox->setItemText(Other, fileDir);
398 }
399 QTimer::singleShot(50, this, [ = ] {
400 m_savePathCombox->setCurrentText(fileDir);
401 });
402 }
403 }
404
405 void CExportImageDialog::showEmptyWarningDialog()
406 {
407 m_emptyWarningDialog->clearContents();
408 DWidget *wid = new DWidget();
409 wid->setFocusPolicy(Qt::NoFocus);
410 // DLabel *lab1 = new DLabel();
411 // QFontMetrics elideFont(lab1->font());
412 DLabel *lab2 = new DLabel();
413 lab2->setText(tr("File name cannot be empty"));
414 lab2->setAlignment(Qt::AlignCenter);
415
416 QVBoxLayout *lay = new QVBoxLayout();
417 lay->setContentsMargins(0, 0, 0, 0);
418 // lay->addWidget(lab1);
419 lay->addWidget(lab2);
420 lay->addSpacing(100);
421 wid->setLayout(lay);
422 m_emptyWarningDialog->addContent(wid, Qt::AlignCenter);
423
424 m_emptyWarningDialog->show();
425 }
426
427 void CExportImageDialog::showQuestionDialog(const QString &path, const QString &srcpath)
428 {
429 //BUG#90251 规避焦点消失问题
430 delete m_questionDialog;
431 m_questionDialog = new DDialog(this);
432 m_questionDialog->setFocusPolicy(Qt::NoFocus);
433 m_questionDialog->setModal(true);
434 m_questionDialog->addButtons({tr("Cancel"), tr("Replace")});
435 m_questionDialog->setFixedSize(400, 170);
436 connect(m_questionDialog, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(slotOnQuestionDialogButtonClick(int, const QString &)));
437 //BUG#90251
438
439 DWidget *wid = new DWidget();
440 DLabel *lab1 = new DLabel();
441 QFontMetrics elideFont(lab1->font());
442 lab1->setText(elideFont.elidedText(path, Qt::ElideRight, 255));
443 lab1->setToolTip(path);
444
445 DLabel *lab2 = new DLabel();
446 lab2->setText(tr("already exists. Do you want to replace it?"));
447 lab2->setAlignment(Qt::AlignCenter);
448
449 QVBoxLayout *lay = new QVBoxLayout();
450 lay->setContentsMargins(0, 0, 0, 0);
451 lay->addWidget(lab1);
452 lay->addWidget(lab2);
453 lay->addSpacing(100);
454 wid->setLayout(lay);
455 m_questionDialog->addContent(wid, Qt::AlignCenter);
456 if (!srcpath.isEmpty()) {
457 m_saveImage = QPixmap(srcpath);
458 }
459 m_savePath = path.left(path.lastIndexOf("/"));
460 // m_questionDialog->setMessage((QString(tr("%1 \already exists, do you want to replace?")).arg(path)));
461 m_questionDialog->move(dApp->getMainWindow()->x() + (dApp->getMainWindow()->width() - m_questionDialog->width()) / 2,
462 dApp->getMainWindow()->y() + (dApp->getMainWindow()->height() - m_questionDialog->height()) / 2);
463 m_questionDialog->exec();
464 }
465
466 bool CExportImageDialog::doSave()
467 {
468 bool isSuccess = false;
469 m_saveFormat = m_formatCombox->currentText();
470 QString completePath = m_savePath + "/" + m_fileNameEdit->text().trimmed() + "." + m_saveFormat;
471 if (tr("gif") == m_saveFormat) {
472 if(gifpath.isEmpty()){
473 return isSuccess;
474 }
475
476 QFileInfo fileinfo(completePath);
477 if (fileinfo.exists() && !fileinfo.isDir()) {
478 //目标位置与原图位置相同则直接返回
479 if(gifpath == completePath){
480 return true;
481 }
482 //目标位置与原图位置不同则先删除再复制
483 if (QFile::remove(completePath)) {
484 isSuccess = QFile::copy(gifpath, completePath);
485 }
486 } else {
487 isSuccess = QFile::copy(gifpath, completePath);
488 }
489 } else {
490 isSuccess = m_saveImage.save(completePath, m_saveFormat.toUpper().toLocal8Bit().data(), m_quality);
491 qDebug() << "!!!!!!!!!" << isSuccess << "::" << completePath << "::" << m_saveFormat;
492 }
493 return isSuccess;
494 }