00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <math.h>
00025
00026 #include <QApplication>
00027 #include <QDesktopWidget>
00028 #include <QIcon>
00029 #include <QPushButton>
00030 #include <QDebug>
00031
00032 #include "klfutil.h"
00033 #include "klfguiutil.h"
00034
00035
00036
00037
00038
00039 KLFProgressReporter::KLFProgressReporter(int min, int max, QObject *parent)
00040 : QObject(parent)
00041 {
00042 pMin = min;
00043 pMax = max;
00044 pFinished = false;
00045 }
00046 KLFProgressReporter::~KLFProgressReporter()
00047 {
00048 if (!pFinished) {
00049 emit progress(pMax);
00050 emit finished();
00051 }
00052 }
00053
00054 void KLFProgressReporter::doReportProgress(int value)
00055 {
00056 if (pFinished) {
00057 qWarning()<<KLF_FUNC_NAME<<": Operation is already finished!";
00058 return;
00059 }
00060 emit progress(value);
00061 if (value == pMax) {
00062 emit finished();
00063 pFinished = true;
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 KLFProgressDialog::KLFProgressDialog(QString labelText, QWidget *parent)
00074 : QProgressDialog(parent)
00075 {
00076 setup(false);
00077 init(labelText);
00078 }
00079 KLFProgressDialog::KLFProgressDialog(bool canCancel, QString labelText, QWidget *parent)
00080 : QProgressDialog(parent)
00081 {
00082 setup(canCancel);
00083 init(labelText);
00084 }
00085 KLFProgressDialog::~KLFProgressDialog()
00086 {
00087 }
00088
00089 void KLFProgressDialog::setup(bool canCancel)
00090 {
00091 pProgressReporter = NULL;
00092 setAutoClose(true);
00093 setAutoReset(true);
00094 setModal(true);
00095
00096 setWindowIcon(QIcon(":/pics/klatexformula-16.png"));
00097 setWindowTitle(tr("Progress"));
00098 QPushButton *cbtn = new QPushButton(tr("Cancel"), this);
00099 setCancelButton(cbtn);
00100 cbtn->setEnabled(canCancel);
00101 }
00102 void KLFProgressDialog::init(const QString& labelText)
00103 {
00104 setDescriptiveText(labelText);
00105 }
00106
00107 void KLFProgressDialog::setDescriptiveText(const QString& labelText)
00108 {
00109 setLabelText(labelText);
00110 setFixedSize((int)(sizeHint().width()*1.3), (int)(sizeHint().height()*1.1));
00111 }
00112 void KLFProgressDialog::startReportingProgress(KLFProgressReporter *progressReporter,
00113 const QString& descriptiveText)
00114 {
00115 reset();
00116 setDescriptiveText(descriptiveText);
00117 setRange(progressReporter->min(), progressReporter->max());
00118 setValue(0);
00119
00120
00121 if (pProgressReporter != NULL)
00122 disconnect(pProgressReporter, 0, this, SLOT(setValue(int)));
00123
00124 connect(progressReporter, SIGNAL(progress(int)), this, SLOT(setValue(int)));
00125 }
00126
00127 void KLFProgressDialog::startReportingProgress(KLFProgressReporter *progressReporter)
00128 {
00129 reset();
00130 setRange(progressReporter->min(), progressReporter->max());
00131 setValue(0);
00132
00133 if (pProgressReporter != NULL)
00134 disconnect(pProgressReporter, 0, this, SLOT(setValue(int)));
00135
00136 connect(progressReporter, SIGNAL(progress(int)), this, SLOT(setValue(int)));
00137 }
00138
00139 void KLFProgressDialog::setValue(int value)
00140 {
00141
00142 klfDbg("value="<<value);
00143 QProgressDialog::setValue(value);
00144 }
00145
00146 void KLFProgressDialog::paintEvent(QPaintEvent *event)
00147 {
00148 KLF_DEBUG_BLOCK(KLF_FUNC_NAME);
00149 QProgressDialog::paintEvent(event);
00150 }
00151
00152
00153
00154
00155
00156 static Qt::WindowFlags klfpleasewait_flagsForSettings(bool alwaysAbove)
00157 {
00158 Qt::WindowFlags f = Qt::Window|Qt::SplashScreen|Qt::FramelessWindowHint;
00159 if (alwaysAbove)
00160 f |= Qt::WindowStaysOnTopHint|Qt::X11BypassWindowManagerHint;
00161 return f;
00162 }
00163
00164 KLFPleaseWaitPopup::KLFPleaseWaitPopup(const QString& text, QWidget *parent, bool alwaysAbove)
00165 : QLabel(text, ((parent!=NULL)?parent->window():NULL), klfpleasewait_flagsForSettings(alwaysAbove)),
00166 pParentWidget(parent), pDisableUi(false), pGotPaintEvent(false), pDiscarded(false)
00167 {
00168 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00169 QFont f = font();
00170 f.setPointSize(QFontInfo(f).pointSize() + 2);
00171 setFont(f);
00172 setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
00173 setWindowModality(Qt::ApplicationModal);
00174
00175 setAttribute(Qt::WA_StyledBackground, true);
00176 setProperty("klfTopLevelWidget", QVariant(true));
00177
00178 setFrameStyle(QFrame::Panel|QFrame::Sunken);
00179
00180 QWidget *pw = parentWidget();
00181 if (pw != NULL)
00182 setStyleSheet(pw->window()->styleSheet());
00183
00184 int w = qMax( (int)(sizeHint().width() *1.3) , 500 );
00185 int h = qMax( (int)(sizeHint().height()*1.1) , 100 );
00186 setFixedSize(w, h);
00187 setWindowOpacity(0.94);
00188 }
00189 KLFPleaseWaitPopup::~KLFPleaseWaitPopup()
00190 {
00191 if (pDisableUi && pParentWidget != NULL)
00192 pParentWidget->setEnabled(true);
00193 }
00194
00195 void KLFPleaseWaitPopup::setDisableUi(bool disableUi)
00196 {
00197 pDisableUi = disableUi;
00198 }
00199
00200 void KLFPleaseWaitPopup::showPleaseWait()
00201 {
00202 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00203
00204 QSize desktopSize;
00205 QDesktopWidget *dw = QApplication::desktop();
00206 if (dw != NULL) {
00207 desktopSize = dw->screenGeometry(this).size();
00208 } else {
00209 desktopSize = QSize(1024, 768);
00210 }
00211 move(desktopSize.width()/2 - width()/2, desktopSize.height()/2 - height()/2);
00212 show();
00213 setStyleSheet(styleSheet());
00214
00215 if (pDisableUi && pParentWidget != NULL)
00216 pParentWidget->setEnabled(false);
00217
00218 while (!pGotPaintEvent)
00219 qApp->processEvents();
00220 }
00221
00222 void KLFPleaseWaitPopup::mousePressEvent(QMouseEvent *)
00223 {
00224 hide();
00225 pDiscarded = true;
00226 }
00227
00228 void KLFPleaseWaitPopup::paintEvent(QPaintEvent *event)
00229 {
00230 pGotPaintEvent = true;
00231 QLabel::paintEvent(event);
00232 }
00233
00234
00235
00236
00237
00238
00239 KLFDelayedPleaseWaitPopup::KLFDelayedPleaseWaitPopup(const QString& text, QWidget *callingWidget)
00240 : KLFPleaseWaitPopup(text, callingWidget), pDelay(1000)
00241 {
00242 timer.start();
00243 }
00244 KLFDelayedPleaseWaitPopup::~KLFDelayedPleaseWaitPopup()
00245 {
00246 }
00247 void KLFDelayedPleaseWaitPopup::setDelay(int ms)
00248 {
00249 pDelay = ms;
00250 }
00251 void KLFDelayedPleaseWaitPopup::process()
00252 {
00253 if (!pleaseWaitShown() && timer.elapsed() > pDelay)
00254 showPleaseWait();
00255 qApp->processEvents();
00256 }
00257
00258
00259
00260
00261
00262
00263 KLFEnumComboBox::KLFEnumComboBox(QWidget *parent)
00264 : QComboBox(parent)
00265 {
00266 setEnumValues(QList<int>(), QStringList());
00267 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(internalCurrentIndexChanged(int)));
00268 }
00269
00270 KLFEnumComboBox::KLFEnumComboBox(const QList<int>& enumValues, const QStringList& enumTitles,
00271 QWidget *parent)
00272 : QComboBox(parent)
00273 {
00274 setEnumValues(enumValues, enumTitles);
00275 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(internalCurrentIndexChanged(int)));
00276 }
00277
00278 KLFEnumComboBox::~KLFEnumComboBox()
00279 {
00280 }
00281
00282 void KLFEnumComboBox::setEnumValues(const QList<int>& enumValues, const QStringList& enumTitles)
00283 {
00284 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00285 klfDbg("enumValues="<<enumValues<<"; enumTitles="<<enumTitles);
00286 blockSignals(true);
00287 int savedCurrentIndex = currentIndex();
00288 if (enumValues.size() != enumTitles.size()) {
00289 qWarning()<<KLF_FUNC_NAME<<": enum value list and enum title list do not match!";
00290 return;
00291 }
00292 pEnumValueList = enumValues;
00293 clear();
00294 int k;
00295 for (k = 0; k < enumValues.size(); ++k) {
00296 pEnumValues[enumValues[k]] = enumTitles[k];
00297 insertItem(k, enumTitles[k], QVariant(enumValues[k]));
00298 pEnumCbxIndexes[enumValues[k]] = k;
00299 }
00300 if (savedCurrentIndex >= 0 && savedCurrentIndex < count())
00301 setCurrentIndex(savedCurrentIndex);
00302 blockSignals(false);
00303 }
00304
00305 int KLFEnumComboBox::selectedValue() const
00306 {
00307 return itemData(currentIndex()).toInt();
00308 }
00309
00310 QString KLFEnumComboBox::enumText(int enumValue) const
00311 {
00312 if (!pEnumValueList.contains(enumValue)) {
00313 qWarning()<<KLF_FUNC_NAME<<": "<<enumValue<<" is not a registered valid enum value!";
00314 return QString();
00315 }
00316 return pEnumValues[enumValue];
00317 }
00318
00319 void KLFEnumComboBox::setSelectedValue(int val)
00320 {
00321 if (!pEnumCbxIndexes.contains(val)) {
00322 qWarning()<<KLF_FUNC_NAME<<": "<<val<<" is not a registered valid enum value!";
00323 return;
00324 }
00325 setCurrentIndex(pEnumCbxIndexes[val]);
00326 }
00327
00328 void KLFEnumComboBox::internalCurrentIndexChanged(int index)
00329 {
00330 emit selectedValueChanged(itemData(index).toInt());
00331 }
00332
00333
00334
00335
00336
00337 KLFWaitAnimationOverlay::KLFWaitAnimationOverlay(QWidget *parent)
00338 : QLabel(parent)
00339 {
00340 pAnimMovie = NULL;
00341
00342
00343
00344
00345
00346 setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
00347
00348 hide();
00349
00350 pAnimTimerId = -1;
00351 pIsWaiting = false;
00352
00353
00354 pWidthPercent = 30;
00355 pHeightPercent = 70;
00356 pPositionXPercent = 50;
00357 pPositionYPercent = 50;
00358
00359 setBackgroundColor(QColor(255,255,255,128));
00360 }
00361
00362 KLFWaitAnimationOverlay::~KLFWaitAnimationOverlay()
00363 {
00364 }
00365
00366 QColor KLFWaitAnimationOverlay::backgroundColor() const
00367 {
00368 return palette().color(QPalette::Window);
00369 }
00370
00371 void KLFWaitAnimationOverlay::setWaitMovie(QMovie *movie)
00372 {
00373 if (pAnimMovie != NULL) {
00374 delete pAnimMovie;
00375 }
00376 pAnimMovie = movie;
00377 pAnimMovie->setParent(this);
00378 }
00379
00380 void KLFWaitAnimationOverlay::setWaitMovie(const QString& filename)
00381 {
00382 QMovie *m = new QMovie(filename);
00383 m->setCacheMode(QMovie::CacheAll);
00384 setWaitMovie(m);
00385 }
00386
00387
00388 void KLFWaitAnimationOverlay::setBackgroundColor(const QColor& c)
00389 {
00390 setStyleSheet(QString("background-color: rgba(%1,%2,%3,%4)")
00391 .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha()));
00392 }
00393
00394
00395 void KLFWaitAnimationOverlay::startWait()
00396 {
00397 if (pIsWaiting)
00398 return;
00399
00400 pIsWaiting = true;
00401 if (pAnimMovie == NULL)
00402 return;
00403
00404 pAnimMovie->jumpToFrame(0);
00405 setPixmap(pAnimMovie->currentPixmap());
00406 setGeometry(calcAnimationLabelGeometry());
00407 show();
00408 update();
00409
00410 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
00411
00412 pAnimTimerId = startTimer(pAnimMovie->nextFrameDelay());
00413 }
00414
00415 void KLFWaitAnimationOverlay::stopWait()
00416 {
00417 if (!pIsWaiting)
00418 return;
00419
00420 hide();
00421
00422 if (pAnimTimerId >= 0)
00423 killTimer(pAnimTimerId);
00424 pAnimTimerId = -1;
00425 pIsWaiting = false;
00426 }
00427
00428 void KLFWaitAnimationOverlay::timerEvent(QTimerEvent *event)
00429 {
00430 if (event->timerId() == pAnimTimerId) {
00431 pAnimMovie->jumpToNextFrame();
00432 setPixmap(pAnimMovie->currentPixmap());
00433 repaint();
00434 return;
00435 }
00436 }
00437
00438 QRect KLFWaitAnimationOverlay::calcAnimationLabelGeometry()
00439 {
00440 QWidget * w = parentWidget();
00441 if (w == NULL) {
00442 qWarning()<<KLF_FUNC_NAME<<": this animation label MUST be used with a parent!";
00443 return QRect();
00444 }
00445 QRect g = w->geometry();
00446 QSize sz = QSize(w->width()*pWidthPercent/100,w->height()*pHeightPercent/100);
00447
00448 klfDbg("parent geometry: "<<g<<"; our size="<<sz) ;
00449
00450 return KLF_DEBUG_TEE( QRect(QPoint( (g.width()-sz.width())*pPositionXPercent/100,
00451 (g.height()-sz.height())*pPositionYPercent/100),
00452 sz) );
00453 }
00454
00455
00456
00457
00458
00459
00460 KLF_EXPORT void klfDrawGlowedImage(QPainter *p, const QImage& foreground, const QColor& glowcol,
00461 int r, bool also_draw_image)
00462 {
00463 QImage fg = foreground;
00464 if (fg.format() != QImage::Format_ARGB32_Premultiplied &&
00465 fg.format() != QImage::Format_ARGB32)
00466 fg = fg.convertToFormat(QImage::Format_ARGB32);
00467
00468 QRgb glow_color = glowcol.rgba();
00469 int ga = qAlpha(glow_color);
00470
00471 QImage glow(fg.size(), QImage::Format_ARGB32_Premultiplied);
00472 int x, y;
00473 for (x = 0; x < fg.width(); ++x) {
00474 for (y = 0; y < fg.height(); ++y) {
00475 int a = qAlpha(fg.pixel(x,y)) * ga / 255;
00476
00477 glow.setPixel(x,y, qRgba(qRed(glow_color)*a/255, qGreen(glow_color)*a/255, qBlue(glow_color)*a/255, a));
00478 }
00479 }
00480
00481 for (x = -r; x <= r; ++x) {
00482 for (y = -r; y <= r; ++y) {
00483 if (x*x+y*y > r*r)
00484 continue;
00485 p->drawImage(QPoint(x,y), glow);
00486 }
00487 }
00488 if (also_draw_image)
00489 p->drawImage(QPoint(0,0), fg);
00490 }