00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "thumbnailview.h"
00015
00016 #include "drawpad.h"
00017 #include "drawpadcanvas.h"
00018 #include "newpagedialog.h"
00019 #include "page.h"
00020
00021 #include <opie2/oresource.h>
00022
00023 #include <qpe/config.h>
00024 #include <qpe/timestring.h>
00025
00026 #include <qapplication.h>
00027 #include <qlayout.h>
00028 #include <qmessagebox.h>
00029 #include <qtoolbutton.h>
00030 #include <qwhatsthis.h>
00031
00032 #define THUMBNAIL_SIZE 48
00033
00034 PageListBoxItem::PageListBoxItem(Page* page, QListBox* parent)
00035 : QListBoxItem(parent)
00036 {
00037 m_pPage = page;
00038
00039 QImage image = m_pPage->pixmap()->convertToImage();
00040
00041 int previewWidth = THUMBNAIL_SIZE;
00042 int previewHeight = THUMBNAIL_SIZE;
00043
00044 float widthScale = 1.0;
00045 float heightScale = 1.0;
00046
00047 if (previewWidth < image.width()) {
00048 widthScale = (float)previewWidth / float(image.width());
00049 }
00050
00051 if (previewHeight < image.height()) {
00052 heightScale = (float)previewHeight / float(image.height());
00053 }
00054
00055 float scale = (widthScale < heightScale ? widthScale : heightScale);
00056 QImage thumbnailImage = image.smoothScale((int)(image.width() * scale) , (int)(image.height() * scale));
00057
00058 m_thumbnail.convertFromImage(thumbnailImage);
00059
00060 m_titleText = QObject::tr( "Title: %1" ).arg( m_pPage->title() );
00061 m_dimensionText = QObject::tr( "Dimension: %1x%2" ).
00062 arg( m_pPage->pixmap()->width() ).
00063 arg( m_pPage->pixmap()->height() );
00064 m_dateText = QObject::tr( "Date: %1" ).arg( dateTimeString(m_pPage->lastModified()) );
00065
00066 QColor baseColor = parent->colorGroup().base();
00067 int h, s, v;
00068 baseColor.hsv(&h, &s, &v);
00069
00070 if (v > 128) {
00071 m_alternateColor = baseColor.dark(115);
00072 } else if (baseColor != Qt::black) {
00073 m_alternateColor = baseColor.light(115);
00074 } else {
00075 m_alternateColor = QColor(32, 32, 32);
00076 }
00077 }
00078
00079 PageListBoxItem::~PageListBoxItem()
00080 {
00081 }
00082
00083 int PageListBoxItem::height(const QListBox*) const
00084 {
00085 return QMAX(THUMBNAIL_SIZE + 4, QApplication::globalStrut().height());
00086 }
00087
00088 int PageListBoxItem::width(const QListBox* lb) const
00089 {
00090 QFontMetrics fontMetrics = lb->fontMetrics();
00091 int maxtextLength = QMAX(fontMetrics.width(m_titleText),
00092 QMAX(fontMetrics.width(m_dimensionText),
00093 fontMetrics.width(m_dateText)));
00094
00095 return QMAX(THUMBNAIL_SIZE + maxtextLength + 8, QApplication::globalStrut().width());
00096 }
00097
00098 void PageListBoxItem::paint(QPainter *painter)
00099 {
00100 QRect itemRect = listBox()->itemRect(this);
00101
00102 if (!selected() && (listBox()->index(this) % 2)) {
00103 painter->fillRect(0, 0, itemRect.width(), itemRect.height(), m_alternateColor);
00104 }
00105
00106 painter->drawPixmap(2 + (THUMBNAIL_SIZE - m_thumbnail.width()) / 2,
00107 2 + (THUMBNAIL_SIZE - m_thumbnail.height()) / 2,
00108 m_thumbnail);
00109
00110 QFont standardFont = painter->font();
00111 QFont boldFont = painter->font();
00112 boldFont.setBold(TRUE);
00113
00114 QFontMetrics fontMetrics = painter->fontMetrics();
00115 QRect textRect(THUMBNAIL_SIZE + 6, 2,
00116 itemRect.width() - THUMBNAIL_SIZE - 8,
00117 itemRect.height() - 4);
00118
00119 painter->setFont(boldFont);
00120 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, m_titleText);
00121
00122 painter->setFont(standardFont);
00123 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_dimensionText);
00124 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom, m_dateText);
00125
00126 if (!selected() && !(listBox()->hasFocus() && listBox()->item(listBox()->currentItem()) == this)) {
00127 painter->drawLine(0, itemRect.height() - 1, itemRect.width() - 1, itemRect.height() - 1);
00128 }
00129 }
00130
00131 Page* PageListBoxItem::page() const
00132 {
00133 return m_pPage;
00134 }
00135
00136 QString PageListBoxItem::dateTimeString(QDateTime dateTime)
00137 {
00138 QString result;
00139
00140 Config config("qpe");
00141 config.setGroup("Date");
00142
00143 QChar separator = config.readEntry("Separator", "/")[0];
00144 DateFormat::Order shortOrder = (DateFormat::Order)config .readNumEntry("ShortOrder", DateFormat::DayMonthYear);
00145
00146 for (int i = 0; i < 3; i++) {
00147 switch((shortOrder >> (i * 3)) & 0x0007) {
00148 case 0x0001:
00149 result.append( QString().sprintf("%02d", dateTime.date().day()) );
00150 break;
00151 case 0x0002:
00152 result.append( QString().sprintf("%02d", dateTime.date().month()) );
00153 break;
00154 case 0x0004:
00155 result.append( QString().sprintf("%04d", dateTime.date().year()) );
00156 break;
00157 default:
00158 break;
00159 }
00160
00161 if (i < 2) {
00162 result.append( separator );
00163 }
00164 }
00165
00166 result.append( QString().sprintf(" %02d:%02d", dateTime.time().hour(), dateTime.time().minute()) );
00167
00168 return result;
00169 }
00170
00171 PageListBox::PageListBox(DrawPadCanvas* drawPadCanvas, QWidget* parent, const char* name)
00172 : QListBox(parent, name)
00173 {
00174 m_pDrawPadCanvas = drawPadCanvas;
00175
00176 setVScrollBarMode(QScrollView::AlwaysOn);
00177
00178 updateView();
00179 }
00180
00181 PageListBox::~PageListBox()
00182 {
00183 }
00184
00185 void PageListBox::updateView()
00186 {
00187 clear();
00188
00189 if (m_pDrawPadCanvas) {
00190 QList<Page> pageList = m_pDrawPadCanvas->pages();
00191 QListIterator<Page> it(pageList);
00192
00193 for (; it.current(); ++it) {
00194 new PageListBoxItem(it.current(), this);
00195 }
00196
00197 select(m_pDrawPadCanvas->currentPage());
00198 }
00199 }
00200
00201 void PageListBox::select(Page* page)
00202 {
00203 uint i = 0;
00204 uint itemCount = count();
00205
00206 while (i < itemCount) {
00207 PageListBoxItem* currentItem = (PageListBoxItem*)item(i);
00208
00209 if (currentItem->page() == page) {
00210 setCurrentItem(currentItem);
00211 break;
00212 }
00213
00214 i++;
00215 }
00216 }
00217
00218 Page* PageListBox::selected() const
00219 {
00220 Page* page;
00221
00222 PageListBoxItem* selectedItem = (PageListBoxItem*)item(currentItem());
00223
00224 if (selectedItem) {
00225 page = selectedItem->page();
00226 } else {
00227 page = NULL;
00228 }
00229
00230 return page;
00231 }
00232
00233 ThumbnailView::ThumbnailView(DrawPad* drawPad, DrawPadCanvas* drawPadCanvas, QWidget* parent, const char* name)
00234 : QWidget(parent, name, Qt::WType_Modal | Qt::WType_TopLevel | Qt::WStyle_ContextHelp)
00235 {
00236 inLoop = false;
00237
00238 m_pDrawPad = drawPad;
00239 m_pDrawPadCanvas = drawPadCanvas;
00240
00241 setCaption(tr("DrawPad - Thumbnail View"));
00242
00243 bool useBigIcon = qApp->desktop()->size().width() > 330;
00244
00245 QToolButton* newPageButton = new QToolButton(this);
00246 newPageButton->setUsesBigPixmap( useBigIcon );
00247 newPageButton->setPixmap(Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon));
00248 newPageButton->setAutoRaise(true);
00249 connect(newPageButton, SIGNAL(clicked()), this, SLOT(newPage()));
00250 QWhatsThis::add( newPageButton, tr( "Click here to add a new sheet." ) );
00251
00252 QToolButton* clearPageButton = new QToolButton(this);
00253 clearPageButton->setUsesBigPixmap( useBigIcon );
00254 clearPageButton->setPixmap(Opie::Core::OResource::loadPixmap("drawpad/clear", Opie::Core::OResource::SmallIcon));
00255 clearPageButton->setAutoRaise(true);
00256 connect(clearPageButton, SIGNAL(clicked()), this, SLOT(clearPage()));
00257 QWhatsThis::add( clearPageButton, tr( "Click here to erase the current sheet." ) );
00258
00259 QToolButton* deletePageButton = new QToolButton(this);
00260 deletePageButton->setUsesBigPixmap( useBigIcon );
00261 deletePageButton->setPixmap(Opie::Core::OResource::loadPixmap("trash", Opie::Core::OResource::SmallIcon));
00262 deletePageButton->setAutoRaise(true);
00263 connect(deletePageButton, SIGNAL(clicked()), this, SLOT(deletePage()));
00264 QWhatsThis::add( deletePageButton, tr( "Click here to remove the current sheet." ) );
00265
00266 m_pMovePageUpButton = new QToolButton(this);
00267 m_pMovePageUpButton->setUsesBigPixmap( useBigIcon );
00268 m_pMovePageUpButton->setPixmap(Opie::Core::OResource::loadPixmap("up", Opie::Core::OResource::SmallIcon));
00269 m_pMovePageUpButton->setAutoRaise(true);
00270 connect(m_pMovePageUpButton, SIGNAL(clicked()), this, SLOT(movePageUp()));
00271 QWhatsThis::add( m_pMovePageUpButton, tr( "Click here to move the current sheet up one position in the list." ) );
00272
00273 m_pMovePageDownButton = new QToolButton(this);
00274 m_pMovePageDownButton->setUsesBigPixmap( useBigIcon );
00275 m_pMovePageDownButton->setPixmap(Opie::Core::OResource::loadPixmap("down", Opie::Core::OResource::SmallIcon));
00276 m_pMovePageDownButton->setAutoRaise(true);
00277 connect(m_pMovePageDownButton, SIGNAL(clicked()), this, SLOT(movePageDown()));
00278 QWhatsThis::add( m_pMovePageDownButton, tr( "Click here to move the current sheet down one position in the list." ) );
00279
00280 m_pPageListBox = new PageListBox(m_pDrawPadCanvas, this);
00281 connect(m_pPageListBox, SIGNAL(selectionChanged()), this, SLOT(changePage()));
00282
00283 QVBoxLayout* mainLayout = new QVBoxLayout(this, 4, 4);
00284 QHBoxLayout* buttonLayout = new QHBoxLayout(0);
00285
00286 buttonLayout->addWidget(newPageButton);
00287 buttonLayout->addWidget(clearPageButton);
00288 buttonLayout->addWidget(deletePageButton);
00289 buttonLayout->addStretch();
00290 buttonLayout->addWidget(m_pMovePageUpButton);
00291 buttonLayout->addWidget(m_pMovePageDownButton);
00292
00293 mainLayout->addLayout(buttonLayout);
00294 mainLayout->addWidget(m_pPageListBox);
00295
00296 updateView();
00297 }
00298
00299 ThumbnailView::~ThumbnailView()
00300 {
00301 hide();
00302 }
00303
00304 void ThumbnailView::updateView()
00305 {
00306 m_pMovePageUpButton->setEnabled(m_pDrawPadCanvas->goPreviousPageEnabled());
00307 m_pMovePageDownButton->setEnabled(m_pDrawPadCanvas->goNextPageEnabled());
00308 }
00309
00310 void ThumbnailView::hide()
00311 {
00312 QWidget::hide();
00313
00314 if (inLoop) {
00315 inLoop = false;
00316 qApp->exit_loop();
00317 }
00318 }
00319
00320 void ThumbnailView::exec()
00321 {
00322 show();
00323
00324 if (!inLoop) {
00325 inLoop = true;
00326 qApp->enter_loop();
00327 }
00328 }
00329
00330 void ThumbnailView::newPage()
00331 {
00332 QRect rect = m_pDrawPadCanvas->contentsRect();
00333
00334 NewPageDialog newPageDialog(rect.width(), rect.height(), m_pDrawPad->pen().color(),
00335 m_pDrawPad->brush().color(), this);
00336
00337 if (newPageDialog.exec() == QDialog::Accepted) {
00338 m_pDrawPadCanvas->newPage(newPageDialog.selectedTitle(), newPageDialog.selectedWidth(),
00339 newPageDialog.selectedHeight(), newPageDialog.selectedColor());
00340 m_pPageListBox->updateView();
00341 updateView();
00342 }
00343 }
00344
00345 void ThumbnailView::clearPage()
00346 {
00347 QMessageBox messageBox(tr("Clear Page"), tr("Do you want to clear\nthe selected page?"),
00348 QMessageBox::Information, QMessageBox::Yes,
00349 QMessageBox::No | QMessageBox::Escape | QMessageBox::Default,
00350 QMessageBox::NoButton, this);
00351
00352 messageBox.setButtonText(QMessageBox::Yes, tr("Yes"));
00353 messageBox.setButtonText(QMessageBox::No, tr("No"));
00354
00355 if (messageBox.exec() == QMessageBox::Yes) {
00356 m_pDrawPadCanvas->clearPage();
00357 m_pPageListBox->updateView();
00358 }
00359 }
00360
00361 void ThumbnailView::deletePage()
00362 {
00363 QMessageBox messageBox(tr("Delete Page"), tr("Do you want to delete\nthe selected page?"),
00364 QMessageBox::Information, QMessageBox::Yes,
00365 QMessageBox::No | QMessageBox::Escape | QMessageBox::Default,
00366 QMessageBox::NoButton, this);
00367
00368 messageBox.setButtonText(QMessageBox::Yes, tr("Yes"));
00369 messageBox.setButtonText(QMessageBox::No, tr("No"));
00370
00371 if (messageBox.exec() == QMessageBox::Yes) {
00372 m_pDrawPadCanvas->deletePage();
00373 m_pPageListBox->updateView();
00374 updateView();
00375 }
00376 }
00377
00378 void ThumbnailView::movePageUp()
00379 {
00380 m_pDrawPadCanvas->movePageUp();
00381 m_pPageListBox->updateView();
00382 updateView();
00383 }
00384
00385 void ThumbnailView::movePageDown()
00386 {
00387 m_pDrawPadCanvas->movePageDown();
00388 m_pPageListBox->updateView();
00389 updateView();
00390 }
00391
00392 void ThumbnailView::changePage()
00393 {
00394 m_pDrawPadCanvas->selectPage(m_pPageListBox->selected());
00395 updateView();
00396 }