1 /* ============================================================
2 *
3 * This file is a part of KDE project
4 *
5 *
6 * Date : 2009-02-08
7 * Description : a kipi plugin to print images
8 *
9 * Copyright 2009-2012 by Angelo Naselli <anaselli at linux dot it>
10 *
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software Foundation;
14 * either version 2, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * ============================================================ */
22
23 #include "printoptionspage.h"
24
25 // Qt includes
26
27 #include <QButtonGroup>
28 #include <QGridLayout>
29 #include <QToolButton>
30 #include <QPushButton>
31 #include <QPainter>
32
33 // KDE includes
34
35 #include <kconfigdialogmanager.h>
36 #include <kconfig.h>
37
38 // Local includes
39
40 #include "tphoto.h"
41 #include "printimagesconfig.h"
42 #include "signalblocker.h"
43 #include "ui_printoptionspage.h"
44 #include "kipiplugins_debug.h"
45
46 namespace KIPIPrintImagesPlugin
47 {
48
49 class PrintOptionsPage::Private
50 : public Ui_PrintOptionsPage
51 {
52 public:
53
54 QWidget* mParent;
55 QList<TPhoto*>* m_photos;
56 int m_currentPhoto;
57 QButtonGroup mScaleGroup;
58 QButtonGroup mPositionGroup;
59 KConfigDialogManager* mConfigDialogManager;
60
61 public:
62
initPositionFrame()63 void initPositionFrame()
64 {
65 mPositionFrame->setStyleSheet
66 (
67 QLatin1String(
68 "QFrame {"
69 " background-color: palette(mid);"
70 " border: 1px solid palette(dark);"
71 "}"
72 "QToolButton {"
73 " border: none;"
74 " background: palette(base);"
75 "}"
76 "QToolButton:hover {"
77 " background: palette(alternate-base);"
78 " border: 1px solid palette(highlight);"
79 "}"
80 "QToolButton:checked {"
81 " background-color: palette(highlight);"
82 "}"
83 )
84 );
85
86 QGridLayout* const layout = new QGridLayout ( mPositionFrame );
87 layout->setContentsMargins( 0, 0, 0, 0 );
88 layout->setSpacing ( 1 );
89
90 for ( int row = 0; row < 3; ++row )
91 {
92 for ( int col = 0; col < 3; ++col )
93 {
94 QToolButton* const button = new QToolButton ( mPositionFrame );
95 button->setFixedSize ( 40, 40 );
96 button->setCheckable ( true );
97 layout->addWidget ( button, row, col );
98
99 Qt::Alignment alignment;
100
101 if ( row == 0 )
102 {
103 alignment = Qt::AlignTop;
104 }
105 else if ( row == 1 )
106 {
107 alignment = Qt::AlignVCenter;
108 }
109 else
110 {
111 alignment = Qt::AlignBottom;
112 }
113 if ( col == 0 )
114 {
115 alignment |= Qt::AlignLeft;
116 }
117 else if ( col == 1 )
118 {
119 alignment |= Qt::AlignHCenter;
120 }
121 else
122 {
123 alignment |= Qt::AlignRight;
124 }
125
126 mPositionGroup.addButton ( button, int ( alignment ) );
127 }
128 }
129 }
130 };
131
PrintOptionsPage(QWidget * const parent,QList<TPhoto * > * const photoList)132 PrintOptionsPage::PrintOptionsPage(QWidget* const parent, QList<TPhoto*>* const photoList)
133 : QWidget(),
134 d(new Private)
135 {
136 d->setupUi ( this );
137 d->mParent = parent;
138 d->m_photos = photoList;
139 d->m_currentPhoto = 0;
140 d->mConfigDialogManager = new KConfigDialogManager ( this, PrintImagesConfig::self() );
141
142 d->initPositionFrame();
143
144 d->mScaleGroup.addButton ( d->mNoScale, NoScale );
145 d->mScaleGroup.addButton ( d->mScaleToPage, ScaleToPage );
146 d->mScaleGroup.addButton ( d->mScaleTo, ScaleToCustomSize );
147 d->mPhotoXPage->setRange ( 0, d->m_photos->size() );
148
149 d->mPX->setSpecialValueText ( i18n ( "disabled" ) );
150 d->mPY->setSpecialValueText ( i18n ( "disabled" ) );
151 d->mPhotoXPage->setSpecialValueText ( i18n ( "disabled" ) );
152
153 //Change cursor to waitCursor during transition
154 QApplication::setOverrideCursor ( QCursor ( Qt::WaitCursor ) );
155
156 // showAdditionalInfo();
157 imagePreview();
158 enableButtons();
159 QApplication::restoreOverrideCursor();
160
161 connect ( d->kcfg_PrintWidth, SIGNAL (valueChanged(double)),
162 SLOT (adjustHeightToRatio()) );
163
164 connect ( d->kcfg_PrintHeight, SIGNAL (valueChanged(double)),
165 SLOT (adjustWidthToRatio()) );
166
167 connect ( d->kcfg_PrintKeepRatio, SIGNAL (toggled(bool)),
168 SLOT (adjustHeightToRatio()) );
169
170 connect ( d->mPhotoXPage, SIGNAL (valueChanged(int)), SLOT (photoXpageChanged(int)) );
171 connect ( d->mPX, SIGNAL (valueChanged(int)), SLOT (horizontalPagesChanged(int)) );
172 connect ( d->mPY, SIGNAL (valueChanged(int)), SLOT (verticalPagesChanged(int)) );
173
174 connect ( d->mRightButton, SIGNAL (clicked()),
175 this, SLOT (selectNext()) );
176
177 connect ( d->mLeftButton, SIGNAL (clicked()),
178 this, SLOT (selectPrev()) );
179
180 connect ( d->mSaveSettings, SIGNAL (clicked()),
181 this, SLOT (saveConfig()) );
182
183 connect ( d->mNoScale, SIGNAL (clicked(bool)), SLOT (scaleOption()) );
184 connect ( d->mScaleToPage, SIGNAL (clicked(bool)), SLOT (scaleOption()) );
185 connect ( d->mScaleTo, SIGNAL (clicked(bool)), SLOT (scaleOption()) );
186 connect ( d->kcfg_PrintAutoRotate, SIGNAL (toggled(bool)), SLOT (autoRotate(bool)) );
187 connect ( &d->mPositionGroup, SIGNAL (buttonClicked(int)), SLOT (positionChosen(int)));
188
189 layout()->setMargin ( 0 );
190 }
191
~PrintOptionsPage()192 PrintOptionsPage::~PrintOptionsPage()
193 {
194 delete d;
195 }
196
unitToInches(PrintOptionsPage::Unit unit)197 double PrintOptionsPage::unitToInches ( PrintOptionsPage::Unit unit )
198 {
199 if ( unit == PrintOptionsPage::Inches )
200 {
201 return 1.;
202 }
203 else if ( unit == PrintOptionsPage::Centimeters )
204 {
205 return 1/2.54;
206 }
207 else // Millimeters
208 {
209 return 1/25.4;
210 }
211 }
212
alignment() const213 Qt::Alignment PrintOptionsPage::alignment() const
214 {
215 int id = d->mPositionGroup.checkedId();
216 // qCDebug(KIPIPLUGINS_LOG) << "alignment=" << id;
217
218 return Qt::Alignment ( id );
219 }
220
scaleUnit() const221 PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const
222 {
223 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mUnit = PrintOptionsPage::Unit ( d->kcfg_PrintUnit->currentIndex() );
224
225 return PrintOptionsPage::Unit ( d->kcfg_PrintUnit->currentIndex() );
226 }
227
scaleWidth() const228 double PrintOptionsPage::scaleWidth() const
229 {
230 d->m_photos->at ( d->m_currentPhoto )->cropRegion = QRect ( 0, 0,
231 ( int ) ( d->kcfg_PrintWidth->value() * unitToInches ( scaleUnit() ) ),
232 ( int ) ( d->kcfg_PrintHeight->value() * unitToInches ( scaleUnit() ) ) );
233
234 return d->kcfg_PrintWidth->value() * unitToInches ( scaleUnit() );
235 }
236
scaleHeight() const237 double PrintOptionsPage::scaleHeight() const
238 {
239 d->m_photos->at ( d->m_currentPhoto )->cropRegion = QRect ( 0, 0,
240 ( int ) ( d->kcfg_PrintWidth->value() * unitToInches ( scaleUnit() ) ),
241 ( int ) ( d->kcfg_PrintHeight->value() * unitToInches ( scaleUnit() ) ) );
242
243 return d->kcfg_PrintHeight->value() * unitToInches ( scaleUnit() );
244 }
245
adjustWidthToRatio()246 void PrintOptionsPage::adjustWidthToRatio()
247 {
248 if ( !d->kcfg_PrintKeepRatio->isChecked() )
249 {
250 return;
251 }
252
253 double width = d->m_photos->at ( d->m_currentPhoto )->width() * d->kcfg_PrintHeight->value() /
254 d->m_photos->at ( d->m_currentPhoto )->height();
255 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintHeight = d->kcfg_PrintHeight->value();
256 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintWidth = width ? width : 1.;
257 SignalBlocker blocker ( d->kcfg_PrintWidth );
258 d->kcfg_PrintWidth->setValue ( d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintWidth );
259 // qCDebug(KIPIPLUGINS_LOG) << " width " << d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintWidth << " height " << d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintHeight;
260 }
261
adjustHeightToRatio()262 void PrintOptionsPage::adjustHeightToRatio()
263 {
264 if ( !d->kcfg_PrintKeepRatio->isChecked() )
265 {
266 return;
267 }
268
269 double height = d->m_photos->at ( d->m_currentPhoto )->height() * d->kcfg_PrintWidth->value() / d->m_photos->at ( d->m_currentPhoto )->width();
270 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintWidth = d->kcfg_PrintWidth->value();
271 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintHeight = height ? height : 1. ;
272 SignalBlocker blocker ( d->kcfg_PrintHeight );
273 d->kcfg_PrintHeight->setValue ( d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintHeight );
274 // qCDebug(KIPIPLUGINS_LOG) << "height " << d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintHeight << " width " << d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintWidth;
275 }
276
manageQPrintDialogChanges(QPrinter *)277 void PrintOptionsPage::manageQPrintDialogChanges ( QPrinter * /*printer*/ )
278 {
279 qCDebug(KIPIPLUGINS_LOG) << "It has been called!";
280 }
281
photoXPage() const282 int PrintOptionsPage::photoXPage() const
283 {
284 return d->mPhotoXPage->value();
285 }
286
mp_horPages() const287 int PrintOptionsPage::mp_horPages() const
288 {
289 return d->mPX->value();
290 }
291
mp_verPages() const292 int PrintOptionsPage::mp_verPages() const
293 {
294 return d->mPY->value();
295 }
printUsingAtkinsLayout() const296 bool PrintOptionsPage::printUsingAtkinsLayout() const
297 {
298 return ( d->mPhotoXPage->value() > 0 );
299 }
300
enableButtons()301 void PrintOptionsPage::enableButtons()
302 {
303 if ( d->m_photos->size() == 1 )
304 {
305 d->mLeftButton->setEnabled ( false );
306 d->mRightButton->setEnabled ( false );
307 }
308 else if ( d->m_currentPhoto == 0 )
309 {
310 d->mLeftButton->setEnabled ( false );
311 d->mRightButton->setEnabled ( true );
312 }
313 else if ( d->m_currentPhoto == d->m_photos->size()-1 )
314 {
315 d->mRightButton->setEnabled ( false );
316 d->mLeftButton->setEnabled ( true );
317 }
318 else
319 {
320 d->mLeftButton->setEnabled ( true );
321 d->mRightButton->setEnabled ( true );
322 }
323 }
324
imagePreview()325 void PrintOptionsPage::imagePreview()
326 {
327 // qCDebug(KIPIPLUGINS_LOG) << d->m_currentPhoto;
328 TPhoto *pPhoto = d->m_photos->at ( d->m_currentPhoto );
329 d->mPreview->setPixmap ( pPhoto->thumbnail() );
330 if ( pPhoto->cropRegion != QRect() )
331 {
332 // TODO
333 }
334 }
335
selectNext()336 void PrintOptionsPage::selectNext()
337 {
338 //Change cursor to waitCursor during transition
339 QApplication::setOverrideCursor ( QCursor ( Qt::WaitCursor ) );
340
341 // qCDebug(KIPIPLUGINS_LOG) << d->m_currentPhoto;
342
343 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintPosition = alignment();
344
345 if ( d->m_currentPhoto+1 < d->m_photos->size() )
346 d->m_currentPhoto++;
347
348 showAdditionalInfo();
349 imagePreview();
350 enableButtons();
351 QApplication::restoreOverrideCursor();
352 }
353
selectPrev()354 void PrintOptionsPage::selectPrev()
355 {
356 //Change cursor to waitCursor during transition
357 QApplication::setOverrideCursor ( QCursor ( Qt::WaitCursor ) );
358
359 // qCDebug(KIPIPLUGINS_LOG) << d->m_currentPhoto;
360 d->m_photos->at ( d->m_currentPhoto )->pAddInfo->mPrintPosition = alignment();
361
362 if ( d->m_currentPhoto-1 >= 0 )
363 d->m_currentPhoto--;
364
365 showAdditionalInfo();
366 imagePreview();
367 enableButtons();
368 QApplication::restoreOverrideCursor();
369 }
370
setAdditionalInfo()371 void PrintOptionsPage::setAdditionalInfo()
372 {
373 for ( int i=0; i < d->m_photos->count(); ++i )
374 {
375 TPhoto* pPhoto = d->m_photos->at ( i );
376
377 if ( pPhoto )
378 {
379 pPhoto->pAddInfo->mUnit = PrintImagesConfig::printUnit();
380 pPhoto->pAddInfo->mPrintPosition = PrintImagesConfig::printPosition();
381 pPhoto->pAddInfo->mKeepRatio = PrintImagesConfig::printKeepRatio();
382 pPhoto->pAddInfo->mScaleMode = PrintImagesConfig::printScaleMode();
383 pPhoto->pAddInfo->mAutoRotate = PrintImagesConfig::printAutoRotate();
384 pPhoto->pAddInfo->mPrintWidth = PrintImagesConfig::printWidth();
385 pPhoto->pAddInfo->mPrintHeight = PrintImagesConfig::printHeight();
386 pPhoto->pAddInfo->mEnlargeSmallerImages = PrintImagesConfig::printEnlargeSmallerImages();
387
388 if (pPhoto->pAddInfo->mKeepRatio)
389 {
390 double height = d->m_photos->at(i)->height() * pPhoto->pAddInfo->mPrintWidth / d->m_photos->at(i)->width();
391 d->m_photos->at(i)->pAddInfo->mPrintHeight = height ? height : PrintImagesConfig::printHeight();
392 }
393 // qCDebug(KIPIPLUGINS_LOG) << " photo " << i << " printWidth " << pPhoto->pAddInfo->mPrintWidth
394 // << " printHeight " << pPhoto->pAddInfo->mPrintHeight;
395 //
396 }
397 }
398 }
399
showAdditionalInfo()400 void PrintOptionsPage::showAdditionalInfo()
401 {
402 QAbstractButton* button = nullptr;
403 int i = d->m_currentPhoto;
404 TPhoto* const pPhoto = d->m_photos->at ( i );
405
406 if ( pPhoto )
407 {
408 d->kcfg_PrintUnit->setCurrentIndex ( pPhoto->pAddInfo->mUnit );
409 button = d->mPositionGroup.button ( pPhoto->pAddInfo->mPrintPosition );
410
411 if ( button )
412 {
413 button->setChecked ( true );
414 }
415 else
416 {
417 qCWarning(KIPIPLUGINS_LOG) << "Unknown button for position group";
418 }
419
420 button = d->mScaleGroup.button ( pPhoto->pAddInfo->mScaleMode );
421
422 if ( button )
423 {
424 button->setChecked ( true );
425 }
426 else
427 {
428 qCWarning(KIPIPLUGINS_LOG) << "Unknown button for scale group";
429 }
430
431 d->kcfg_PrintKeepRatio->setChecked ( pPhoto->pAddInfo->mKeepRatio );
432 d->kcfg_PrintAutoRotate->setChecked ( pPhoto->pAddInfo->mAutoRotate );
433 d->kcfg_PrintEnlargeSmallerImages->setChecked ( pPhoto->pAddInfo->mEnlargeSmallerImages );
434 d->kcfg_PrintWidth->setValue ( pPhoto->pAddInfo->mPrintWidth );
435 d->kcfg_PrintHeight->setValue ( pPhoto->pAddInfo->mPrintHeight );
436
437 if ( d->kcfg_PrintKeepRatio->isChecked() )
438 {
439 adjustHeightToRatio();
440 }
441 }
442 }
443
loadConfig()444 void PrintOptionsPage::loadConfig()
445 {
446 QAbstractButton* button = nullptr;
447 button = d->mPositionGroup.button ( PrintImagesConfig::printPosition() );
448
449 if ( button )
450 {
451 button->setChecked ( true );
452 }
453 else
454 {
455 qCWarning(KIPIPLUGINS_LOG) << "Unknown button for position group";
456 }
457
458 button = d->mScaleGroup.button ( PrintImagesConfig::printScaleMode() );
459
460 if ( button )
461 {
462 button->setChecked ( true );
463 }
464 else
465 {
466 qCWarning(KIPIPLUGINS_LOG) << "Unknown button for scale group";
467 }
468
469 d->mConfigDialogManager->updateWidgets();
470
471 // config has been read, now we set photo additional info
472 setAdditionalInfo();
473 }
474
saveConfig()475 void PrintOptionsPage::saveConfig()
476 {
477 int position = d->mPositionGroup.checkedId();
478 PrintImagesConfig::setPrintPosition ( position );
479
480 ScaleMode scaleMode = ScaleMode ( d->mScaleGroup.checkedId() );
481 PrintImagesConfig::setPrintScaleMode ( scaleMode );
482
483 bool checked = d->kcfg_PrintAutoRotate->isChecked();
484 PrintImagesConfig::setPrintAutoRotate ( checked );
485
486 d->mConfigDialogManager->updateSettings();
487
488 PrintImagesConfig::self()->save();
489 }
490
photoXpageChanged(int i)491 void PrintOptionsPage::photoXpageChanged ( int i )
492 {
493 bool disabled = ( i>0 );
494 d->mPositionFrame->setDisabled ( disabled );
495 d->mGroupScaling->setDisabled ( disabled );
496 d->mGroupImage->setDisabled ( disabled );
497 d->kcfg_PrintAutoRotate->setDisabled ( disabled );
498 d->mPreview->setDisabled ( disabled );
499
500 if ( disabled )
501 {
502 d->mRightButton->setDisabled ( disabled );
503 d->mLeftButton->setDisabled ( disabled );
504 SignalBlocker block_mPX ( d->mPX );
505 d->mPX->setValue ( 0 );
506 SignalBlocker block_mPY ( d->mPY );
507 d->mPY->setValue ( 0 );
508 }
509 else
510 {
511 enableButtons();
512 }
513 }
514
horizontalPagesChanged(int i)515 void PrintOptionsPage::horizontalPagesChanged ( int i )
516 {
517 bool disabled = ( i>0 );
518 d->mPositionFrame->setDisabled ( disabled );
519 d->mGroupScaling->setDisabled ( disabled );
520 d->mGroupImage->setDisabled ( disabled );
521 d->kcfg_PrintAutoRotate->setDisabled ( disabled );
522 d->mPreview->setDisabled ( disabled );
523
524 if ( disabled )
525 {
526 d->mRightButton->setDisabled ( disabled );
527 d->mLeftButton->setDisabled ( disabled );
528 SignalBlocker blocker ( d->mPhotoXPage );
529 d->mPhotoXPage->setValue ( 0 );
530
531 if ( d->mPY->value() == 0 )
532 {
533 SignalBlocker block_mPY ( d->mPY );
534 d->mPY->setValue ( 1 );
535 }
536 }
537 else
538 {
539 SignalBlocker block_mPX ( d->mPY );
540 d->mPY->setValue ( 0 );
541 enableButtons();
542 }
543 }
544
verticalPagesChanged(int i)545 void PrintOptionsPage::verticalPagesChanged ( int i )
546 {
547 bool disabled = ( i>0 );
548 d->mPositionFrame->setDisabled ( disabled );
549 d->mGroupScaling->setDisabled ( disabled );
550 d->mGroupImage->setDisabled ( disabled );
551 d->kcfg_PrintAutoRotate->setDisabled ( disabled );
552 d->mPreview->setDisabled ( disabled );
553
554 if ( disabled )
555 {
556 d->mRightButton->setDisabled ( disabled );
557 d->mLeftButton->setDisabled ( disabled );
558 SignalBlocker blocker ( d->mPhotoXPage );
559 d->mPhotoXPage->setValue ( 0 );
560
561 if ( d->mPX->value() == 0 )
562 {
563 SignalBlocker block_mPX ( d->mPX );
564 d->mPX->setValue ( 1 );
565 }
566 }
567 else
568 {
569 SignalBlocker block_mPX ( d->mPX );
570 d->mPX->setValue ( 0 );
571 enableButtons();
572 }
573 }
574
scaleOption()575 void PrintOptionsPage::scaleOption()
576 {
577 ScaleMode scaleMode = ScaleMode ( d->mScaleGroup.checkedId() );
578 // qCDebug(KIPIPLUGINS_LOG) << "ScaleMode " << int ( scaleMode );
579 int i = d->m_currentPhoto;
580 TPhoto* const pPhoto = d->m_photos->at ( i );
581
582 if (pPhoto)
583 {
584 pPhoto->pAddInfo->mScaleMode = scaleMode;
585 }
586
587 if (scaleMode == ScaleToCustomSize && d->kcfg_PrintKeepRatio->isChecked())
588 {
589 adjustHeightToRatio();
590 }
591 }
592
autoRotate(bool value)593 void PrintOptionsPage::autoRotate ( bool value )
594 {
595 int i = d->m_currentPhoto;
596 TPhoto* const pPhoto = d->m_photos->at ( i );
597
598 if ( pPhoto )
599 {
600 pPhoto->pAddInfo->mAutoRotate = value;
601 }
602 }
603
positionChosen(int id)604 void PrintOptionsPage::positionChosen(int id)
605 {
606 // qCDebug(KIPIPLUGINS_LOG) << "Current photo " << d->m_currentPhoto << "position " << id;
607 d->m_photos->at(d->m_currentPhoto)->pAddInfo->mPrintPosition = Qt::Alignment(id);
608 }
609
610 } // namespace KIPIPrintImagesPlugin
611