[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfdisplaylabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfdisplaylabel.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: klfdisplaylabel.cpp 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 #include <QLabel>
25 #include <QDir>
26 #include <QTemporaryFile>
27 #include <QMessageBox>
28 #include <QVariant>
29 #include <QPainter>
30 
31 #include <klfguiutil.h>
32 #include "klfdisplaylabel.h"
33 
34 
36  : QLabel(parent), pEnableToolTipPreview(true), mToolTipFile(NULL)
37 {
38  setText(QString());
39  setLabelFixedSize(QSize(120,80));
40 
41  setAlignment(Qt::AlignCenter);
42 
43  pDefaultPalette = palette();
44  pErrorPalette = pDefaultPalette;
45  pErrorPalette.setColor(QPalette::Window, QColor(255, 200, 200));
46 
47  pGE = false;
48  pGEcolor = QColor(128, 255, 128, 8);
49  pGEradius = 4;
50 }
51 
53 {
54  if (mToolTipFile)
55  delete mToolTipFile;
56 }
57 
59 {
60  pLabelFixedSize = size;
61  setMinimumSize(size);
62  setFixedSize(size);
63 }
64 
65 
67 {
68  setPixmap(QPixmap());
69  setText(QString());
70  setEnabled(false);
71  set_error(false);
72 }
73 
74 void KLFDisplayLabel::display(QImage displayimg, QImage tooltipimage, bool labelenabled)
75 {
76  QImage img = displayimg;
77  QPixmap pix;
78  if (labelenabled && pGE) {
79  int r = pGEradius;
80  QSize msz = QSize(2*r, 2*r);
81  if (img.width()+msz.width() > width() || img.height()+msz.height() > height())
82  img = displayimg.scaled(size()-msz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
83  pix = QPixmap(img.size()+msz);
84  pix.fill(QColor(0,0,0,0));
85  QPainter painter(&pix);
86  painter.translate(QPoint(r, r));
87  klfDrawGlowedImage(&painter, img, pGEcolor, r);
88  } else {
89  if (img.width() > width() || img.height() > height())
90  img = displayimg.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
91  pix = QPixmap::fromImage(img);
92  }
93  setPixmap(pix);
94 
95  // un-set any error
96  set_error(false);
97 
98  if (mToolTipFile) {
99  delete mToolTipFile;
100  mToolTipFile = 0;
101  }
102  // no big preview by default
103  _bigPreviewText = "";
104  // but if one is given then prepare it (prepare it even if "enableToolTipPreview" is false,
105  // because we will need it for the "showBigPreview" button)
106  if ( ! tooltipimage.isNull() ) {
107  QString tempdir = QDir::tempPath();
108  mToolTipFile = new QTemporaryFile(tempdir+"/klf_tooltip_XXXXXX.png", this);
109  if ( ! mToolTipFile->open() ) {
110  qWarning("WARNING: Failed open for ToolTip Temp Image!\n%s\n",
111  qPrintable(mToolTipFile->fileTemplate()));
112  delete mToolTipFile;
113  mToolTipFile = 0;
114  } else {
115  mToolTipFile->setAutoRemove(true);
116  bool res = tooltipimage.save(mToolTipFile, "PNG");
117  if ( ! res ) {
118  QMessageBox::critical(this, tr("Error"), tr("Failed write to ToolTip Temp Image file %1!")
119  .arg(mToolTipFile->fileName()));
120  qWarning("WARNING: Failed write to Tooltip temp image to temporary file `%s' !\n",
121  qPrintable(mToolTipFile->fileTemplate()));
122  delete mToolTipFile;
123  mToolTipFile = 0;
124  } else {
125  _bigPreviewText = QString("<img src=\"%1\">").arg(mToolTipFile->fileName());
126  }
127  }
128  }
129  if (pEnableToolTipPreview) {
130  setToolTip(QString("<p style=\"padding: 8px 8px 8px 8px;\">%1</p>").arg(_bigPreviewText));
131  } else {
132  setToolTip(QString(""));
133  }
134 
135  setEnabled(labelenabled);
136 }
137 
138 void KLFDisplayLabel::displayError(bool labelenabled)
139 {
140  set_error(true);
141  setEnabled(labelenabled);
142 }
143 
144 
145 void KLFDisplayLabel::set_error(bool error_on)
146 {
147  setProperty("realTimeLatexError", QVariant(error_on));
148  QPalette *p;
149  if (error_on) {
150  p = &pErrorPalette;
151  } else {
152  p = &pDefaultPalette;
153  }
154  setAutoFillBackground(true);
155  setStyleSheet(styleSheet()); // force style sheet refresh
156  setPalette(*p);
157 }
158 
160 {
161  emit labelDrag();
162 }
virtual ~KLFDisplayLabel()
fill(const QColor &fillColor=Qt::white)
setColor(ColorGroup group, ColorRole role, const QColor &color)
save(const QString &fileName, const char *format=0, int quality=-1)
arg(const QString &a, int fieldWidth=0, const QChar &fillChar=QLatin1Char( ' ')
fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
virtual void displayClear()
tempPath()
setAutoRemove(bool b)
virtual void display(QImage displayimg, QImage tooltipimage, bool labelenabled=true)
setAlignment(int alignment)
virtual void setLabelFixedSize(const QSize &size)
KLFDisplayLabel(QWidget *parent)
scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode=Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode=Qt::FastTransformation)
virtual void mouseMoveEvent(QMouseEvent *e)
critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
translate(const QPointF &offset)
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
virtual void displayError(bool labelenabled=false)

Generated by doxygen 1.8.6