[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfguiutil.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfguiutil.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfguiutil.cpp 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 #include <math.h>
25 
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QIcon>
29 #include <QPushButton>
30 #include <QDebug>
31 
32 #include "klfutil.h"
33 #include "klfguiutil.h"
34 
35 
36 // ----------------------------------------------
37 
38 
40  : QObject(parent)
41 {
42  pMin = min;
43  pMax = max;
44  pFinished = false;
45 }
47 {
48  if (!pFinished) { // make sure finished() is emitted.
49  emit progress(pMax); // some connected clients just wait for maximum value progress
50  emit finished();
51  }
52 }
53 
55 {
56  if (pFinished) {
57  qWarning()<<KLF_FUNC_NAME<<": Operation is already finished!";
58  return;
59  }
60  emit progress(value);
61  if (value == pMax) {
62  emit finished();
63  pFinished = true;
64  }
65 }
66 
67 
68 
69 // ---------------------------------------------------------
70 
71 
72 
74  : QProgressDialog(parent)
75 {
76  setup(false);
77  init(labelText);
78 }
79 KLFProgressDialog::KLFProgressDialog(bool canCancel, QString labelText, QWidget *parent)
80  : QProgressDialog(parent)
81 {
82  setup(canCancel);
83  init(labelText);
84 }
86 {
87 }
88 
89 void KLFProgressDialog::setup(bool canCancel)
90 {
91  pProgressReporter = NULL;
92  setAutoClose(true);
93  setAutoReset(true);
94  setModal(true);
95  // setWindowModality(Qt::ApplicationModal);
96  setWindowIcon(QIcon(":/pics/klatexformula-16.png"));
97  setWindowTitle(tr("Progress"));
98  QPushButton *cbtn = new QPushButton(tr("Cancel"), this);
99  setCancelButton(cbtn);
100  cbtn->setEnabled(canCancel);
101 }
102 void KLFProgressDialog::init(const QString& labelText)
103 {
104  setDescriptiveText(labelText);
105 }
106 
108 {
109  setLabelText(labelText);
110  setFixedSize((int)(sizeHint().width()*1.3), (int)(sizeHint().height()*1.1));
111 }
113  const QString& descriptiveText)
114 {
115  reset();
116  setDescriptiveText(descriptiveText);
117  setRange(progressReporter->min(), progressReporter->max());
118  setValue(0);
119 
120  // disconnect any previous progress reporter object
121  if (pProgressReporter != NULL)
122  disconnect(pProgressReporter, 0, this, SLOT(setValue(int)));
123  // and connect to this new one
124  connect(progressReporter, SIGNAL(progress(int)), this, SLOT(setValue(int)));
125 }
126 
128 {
129  reset();
130  setRange(progressReporter->min(), progressReporter->max());
131  setValue(0);
132  // disconnect any previous progress reporter object
133  if (pProgressReporter != NULL)
134  disconnect(pProgressReporter, 0, this, SLOT(setValue(int)));
135  // and connect to this new one
136  connect(progressReporter, SIGNAL(progress(int)), this, SLOT(setValue(int)));
137 }
138 
140 {
141  // KLF_DEBUG_BLOCK(KLF_FUNC_NAME);
142  klfDbg("value="<<value);
143  QProgressDialog::setValue(value);
144 }
145 
147 {
149  QProgressDialog::paintEvent(event);
150 }
151 
152 
153 // --------------------------
154 
155 
156 static Qt::WindowFlags klfpleasewait_flagsForSettings(bool alwaysAbove)
157 {
158  Qt::WindowFlags f = Qt::Window|Qt::SplashScreen|Qt::FramelessWindowHint;
159  if (alwaysAbove)
160  f |= Qt::WindowStaysOnTopHint|Qt::X11BypassWindowManagerHint;
161  return f;
162 }
163 
164 KLFPleaseWaitPopup::KLFPleaseWaitPopup(const QString& text, QWidget *parent, bool alwaysAbove)
165  : QLabel(text, ((parent!=NULL)?parent->window():NULL), klfpleasewait_flagsForSettings(alwaysAbove)),
166  pParentWidget(parent), pDisableUi(false), pGotPaintEvent(false), pDiscarded(false)
167 {
169  QFont f = font();
170  f.setPointSize(QFontInfo(f).pointSize() + 2);
171  setFont(f);
172  setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
173  setWindowModality(Qt::ApplicationModal);
174  // let this window be styled by skins
175  setAttribute(Qt::WA_StyledBackground, true);
176  setProperty("klfTopLevelWidget", QVariant(true));
177 
178  setFrameStyle(QFrame::Panel|QFrame::Sunken);
179 
180  QWidget *pw = parentWidget(); // the one set in QLabel constructor, this is the top-level window
181  if (pw != NULL)
182  setStyleSheet(pw->window()->styleSheet());
183 
184  int w = qMax( (int)(sizeHint().width() *1.3) , 500 );
185  int h = qMax( (int)(sizeHint().height()*1.1) , 100 );
186  setFixedSize(w, h);
187  setWindowOpacity(0.94);
188 }
190 {
191  if (pDisableUi && pParentWidget != NULL)
192  pParentWidget->setEnabled(true);
193 }
194 
196 {
197  pDisableUi = disableUi;
198 }
199 
201 {
203 
204  QSize desktopSize;
206  if (dw != NULL) {
207  desktopSize = dw->screenGeometry(this).size();
208  } else {
209  desktopSize = QSize(1024, 768); // assume some default, worst case widget is more left and higher
210  }
211  move(desktopSize.width()/2 - width()/2, desktopSize.height()/2 - height()/2);
212  show();
213  setStyleSheet(styleSheet());
214 
215  if (pDisableUi && pParentWidget != NULL)
216  pParentWidget->setEnabled(false);
217 
218  while (!pGotPaintEvent)
219  qApp->processEvents();
220 }
221 
223 {
224  hide();
225  pDiscarded = true;
226 }
227 
229 {
230  pGotPaintEvent = true;
231  QLabel::paintEvent(event);
232 }
233 
234 
235 
236 // --------------------------
237 
238 
240  : KLFPleaseWaitPopup(text, callingWidget), pDelay(1000)
241 {
242  timer.start();
243 }
245 {
246 }
248 {
249  pDelay = ms;
250 }
252 {
253  if (!pleaseWaitShown() && timer.elapsed() > pDelay)
254  showPleaseWait();
255  qApp->processEvents();
256 }
257 
258 
259 
260 // ------------------------------------------------
261 
262 
264  : QComboBox(parent)
265 {
267  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(internalCurrentIndexChanged(int)));
268 }
269 
270 KLFEnumComboBox::KLFEnumComboBox(const QList<int>& enumValues, const QStringList& enumTitles,
271  QWidget *parent)
272  : QComboBox(parent)
273 {
274  setEnumValues(enumValues, enumTitles);
275  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(internalCurrentIndexChanged(int)));
276 }
277 
279 {
280 }
281 
282 void KLFEnumComboBox::setEnumValues(const QList<int>& enumValues, const QStringList& enumTitles)
283 {
285  klfDbg("enumValues="<<enumValues<<"; enumTitles="<<enumTitles);
286  blockSignals(true);
287  int savedCurrentIndex = currentIndex();
288  if (enumValues.size() != enumTitles.size()) {
289  qWarning()<<KLF_FUNC_NAME<<": enum value list and enum title list do not match!";
290  return;
291  }
292  pEnumValueList = enumValues;
293  clear();
294  int k;
295  for (k = 0; k < enumValues.size(); ++k) {
296  pEnumValues[enumValues[k]] = enumTitles[k];
297  insertItem(k, enumTitles[k], QVariant(enumValues[k]));
298  pEnumCbxIndexes[enumValues[k]] = k;
299  }
300  if (savedCurrentIndex >= 0 && savedCurrentIndex < count())
301  setCurrentIndex(savedCurrentIndex);
302  blockSignals(false);
303 }
304 
306 {
307  return itemData(currentIndex()).toInt();
308 }
309 
310 QString KLFEnumComboBox::enumText(int enumValue) const
311 {
312  if (!pEnumValueList.contains(enumValue)) {
313  qWarning()<<KLF_FUNC_NAME<<": "<<enumValue<<" is not a registered valid enum value!";
314  return QString();
315  }
316  return pEnumValues[enumValue];
317 }
318 
320 {
321  if (!pEnumCbxIndexes.contains(val)) {
322  qWarning()<<KLF_FUNC_NAME<<": "<<val<<" is not a registered valid enum value!";
323  return;
324  }
325  setCurrentIndex(pEnumCbxIndexes[val]);
326 }
327 
328 void KLFEnumComboBox::internalCurrentIndexChanged(int index)
329 {
330  emit selectedValueChanged(itemData(index).toInt());
331 }
332 
333 
334 // ------------------------
335 
336 
338  : QLabel(parent)
339 {
340  pAnimMovie = NULL;
341  /*
342  pAnimMovie = new QMovie(":/pics/wait_anim.mng", "MNG", this);
343  pAnimMovie->setCacheMode(QMovie::CacheAll);
344  */
345 
346  setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
347 
348  hide();
349 
350  pAnimTimerId = -1;
351  pIsWaiting = false;
352 
353  // default values
354  pWidthPercent = 30;
355  pHeightPercent = 70;
356  pPositionXPercent = 50;
357  pPositionYPercent = 50;
358 
359  setBackgroundColor(QColor(255,255,255,128));
360 }
361 
363 {
364 }
365 
367 {
368  return palette().color(QPalette::Window);
369 }
370 
372 {
373  if (pAnimMovie != NULL) {
374  delete pAnimMovie;
375  }
376  pAnimMovie = movie;
377  pAnimMovie->setParent(this);
378 }
379 
381 {
382  QMovie *m = new QMovie(filename);
383  m->setCacheMode(QMovie::CacheAll);
384  setWaitMovie(m);
385 }
386 
387 
389 {
390  setStyleSheet(QString("background-color: rgba(%1,%2,%3,%4)")
391  .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha()));
392 }
393 
394 
396 {
397  if (pIsWaiting)
398  return;
399 
400  pIsWaiting = true;
401  if (pAnimMovie == NULL)
402  return;
403 
404  pAnimMovie->jumpToFrame(0);
405  setPixmap(pAnimMovie->currentPixmap());
406  setGeometry(calcAnimationLabelGeometry());
407  show();
408  update();
409 
410  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
411 
412  pAnimTimerId = startTimer(pAnimMovie->nextFrameDelay());
413 }
414 
416 {
417  if (!pIsWaiting)
418  return;
419 
420  hide();
421 
422  if (pAnimTimerId >= 0)
423  killTimer(pAnimTimerId);
424  pAnimTimerId = -1;
425  pIsWaiting = false;
426 }
427 
429 {
430  if (event->timerId() == pAnimTimerId) {
431  pAnimMovie->jumpToNextFrame();
432  setPixmap(pAnimMovie->currentPixmap());
433  repaint();
434  return;
435  }
436 }
437 
439 {
440  QWidget * w = parentWidget();
441  if (w == NULL) {
442  qWarning()<<KLF_FUNC_NAME<<": this animation label MUST be used with a parent!";
443  return QRect();
444  }
445  QRect g = w->geometry();
446  QSize sz = QSize(w->width()*pWidthPercent/100,w->height()*pHeightPercent/100);
447 
448  klfDbg("parent geometry: "<<g<<"; our size="<<sz) ;
449 
450  return KLF_DEBUG_TEE( QRect(QPoint( (g.width()-sz.width())*pPositionXPercent/100,
451  (g.height()-sz.height())*pPositionYPercent/100),
452  sz) );
453 }
454 
455 
456 
457 // -----------------------
458 
459 
460 KLF_EXPORT void klfDrawGlowedImage(QPainter *p, const QImage& foreground, const QColor& glowcol,
461  int r, bool also_draw_image)
462 {
463  QImage fg = foreground;
464  if (fg.format() != QImage::Format_ARGB32_Premultiplied &&
465  fg.format() != QImage::Format_ARGB32)
466  fg = fg.convertToFormat(QImage::Format_ARGB32);
467 
468  QRgb glow_color = glowcol.rgba();
469  int ga = qAlpha(glow_color);
470 
471  QImage glow(fg.size(), QImage::Format_ARGB32_Premultiplied);
472  int x, y;
473  for (x = 0; x < fg.width(); ++x) {
474  for (y = 0; y < fg.height(); ++y) {
475  int a = qAlpha(fg.pixel(x,y)) * ga / 255;
476  // glow format is argb32_premultiplied
477  glow.setPixel(x,y, qRgba(qRed(glow_color)*a/255, qGreen(glow_color)*a/255, qBlue(glow_color)*a/255, a));
478  }
479  }
480  // now draw that glowed image a few times moving around the interest point to do a glow effect
481  for (x = -r; x <= r; ++x) {
482  for (y = -r; y <= r; ++y) {
483  if (x*x+y*y > r*r) // don't go beyond r pixels from (0,0)
484  continue;
485  p->drawImage(QPoint(x,y), glow);
486  }
487  }
488  if (also_draw_image)
489  p->drawImage(QPoint(0,0), fg);
490 }
void doReportProgress(int value)
Definition: klfguiutil.cpp:54
convertToFormat(Format format, Qt::ImageConversionFlags flags=Qt::AutoColor)
virtual void setDelay(int ms)
Definition: klfguiutil.cpp:247
setPointSize(int pointSize)
screenGeometry(int screen=-1)
QColor backgroundColor() const
virtual void paintEvent(QPaintEvent *event)
Definition: klfguiutil.cpp:228
contains(const Key &key)
virtual void startReportingProgress(KLFProgressReporter *progressReporter, const QString &descriptiveText)
Definition: klfguiutil.cpp:112
void paintEvent(QPaintEvent *event)
Definition: klfguiutil.cpp:146
currentPixmap()
virtual bool pleaseWaitShown() const
Definition: klfguiutil.h:272
jumpToNextFrame()
virtual ~KLFPleaseWaitPopup()
Definition: klfguiutil.cpp:189
KLFProgressReporter(int min, int max, QObject *parent=NULL)
Definition: klfguiutil.cpp:39
#define KLF_DEBUG_TEE(expr)
virtual void mousePressEvent(QMouseEvent *event)
Definition: klfguiutil.cpp:222
A popup screen inviting the user to wait.
Definition: klfguiutil.h:237
#define klfDbg(streamableItems)
void setBackgroundColor(const QColor &c)
Set the label background color.
Definition: klfguiutil.cpp:388
#define KLF_DEBUG_BLOCK(msg)
pixel(const QPoint &position)
int min() const
Definition: klfguiutil.h:71
setCancelButton(QPushButton *cancelButton)
insertItem(int index, const QString &text, const QVariant &userData=QVariant()
KLFPleaseWaitPopup(const QString &text, QWidget *callingWidget=NULL, bool alwaysAbove=false)
Definition: klfguiutil.cpp:164
virtual void setValue(int value)
Definition: klfguiutil.cpp:139
void setEnumValues(const QList< int > &enumValues, const QStringList &enumTitles)
Definition: klfguiutil.cpp:282
virtual ~KLFEnumComboBox()
Definition: klfguiutil.cpp:278
virtual ~KLFProgressReporter()
Definition: klfguiutil.cpp:46
virtual void setDisableUi(bool disableUi)
Definition: klfguiutil.cpp:195
KLFWaitAnimationOverlay(QWidget *parent)
Definition: klfguiutil.cpp:337
virtual void setDescriptiveText(const QString &labelText)
Definition: klfguiutil.cpp:107
virtual ~KLFProgressDialog()
Definition: klfguiutil.cpp:85
setAlignment(int alignment)
KLFProgressDialog(QString labelText=QString(), QWidget *parent=NULL)
Definition: klfguiutil.cpp:73
virtual ~KLFDelayedPleaseWaitPopup()
Definition: klfguiutil.cpp:244
setRange(int minimum, int maximum)
KLFEnumComboBox(QWidget *parent=0)
Definition: klfguiutil.cpp:263
static Qt::WindowFlags klfpleasewait_flagsForSettings(bool alwaysAbove)
Definition: klfguiutil.cpp:156
itemData(int index, int role=Qt::UserRole)
#define KLF_FUNC_NAME
contains(const T &value)
QString enumText(int enumValue) const
Definition: klfguiutil.cpp:310
jumpToFrame(int frameNumber)
virtual void timerEvent(QTimerEvent *event)
Definition: klfguiutil.cpp:428
setPixel(const QPoint &position, uint index_or_rgb)
drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags=Qt::AutoColor)
void selectedValueChanged(int enumValue)
KLFDelayedPleaseWaitPopup(const QString &text, QWidget *callingWidget=NULL)
Definition: klfguiutil.cpp:239
virtual ~KLFWaitAnimationOverlay()
Definition: klfguiutil.cpp:362
void setSelectedValue(int val)
Definition: klfguiutil.cpp:319
virtual void showPleaseWait()
Definition: klfguiutil.cpp:200
virtual void setWaitMovie(QMovie *movie)
Set which animation to display while searching.
Definition: klfguiutil.cpp:371
virtual void stopWait()
Hide the animation.
Definition: klfguiutil.cpp:415
KLF_EXPORT void klfDrawGlowedImage(QPainter *p, const QImage &foreground, const QColor &glowcol, int r, bool also_draw_image)
Draws the given image with a glow effect.
Definition: klfguiutil.cpp:460
nextFrameDelay()
int max() const
Definition: klfguiutil.h:72
virtual QRect calcAnimationLabelGeometry()
Definition: klfguiutil.cpp:438
int selectedValue() const
virtual void startWait()
Display the animation.
Definition: klfguiutil.cpp:395
Object that emits progress information of a (lengthy) operation.
Definition: klfguiutil.h:63
currentIndexChanged(int index)
void progress(int progressValue)

Generated by doxygen 1.8.11