[KLF Backend][KLF Tools][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$ */
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 //don't set this to true, because otherwise resizing the label distorts the image
44 //setScaledContents(true);
45
46 pDefaultPalette = palette();
47 pErrorPalette = pDefaultPalette;
48
49 pDefaultPalette.setColor(QPalette::Window, QColor(255, 255, 255, 0)); // fully transparent
50 pErrorPalette.setColor(QPalette::Window, QColor(255, 0, 0, 60)); // red color, semi-transparent
51
52 pGE = false;
53 pGEcolor = QColor(128, 255, 128, 8);
54 pGEradius = 4;
55}
56
58{
59 if (mToolTipFile)
60 delete mToolTipFile;
61}
62
63/*
64void KLFDisplayLabel::setLabelFixedSize(const QSize& size)
65{
66 pLabelFixedSize = size;
67 setMinimumSize(size);
68 setFixedSize(size);
69}
70*/
71
73{
74 display_state(Clear);
75 // setEnabled(false);
76 pLabelEnabled = false;
77}
78
79void KLFDisplayLabel::display(QImage displayimg, QImage tooltipimage, bool labelenabled)
80{
81 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
82
83 pDisplayImage = displayimg;
84 pDisplayTooltip = tooltipimage;
85
86 pLabelEnabled = labelenabled;
87 display_state(Ok);
88}
89
90void KLFDisplayLabel::displayError(const QString& errorMessage, bool labelenabled)
91{
92 pDisplayError = errorMessage;
93
94 pLabelEnabled = labelenabled;
95 display_state(Error);
96}
97
98
99QPicture KLFDisplayLabel::calc_display_picture()
100{
101 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
102
103 double dpr = devicePixelRatioF();
104
105 QImage img = pDisplayImage;
106 QPixmap pix;
107 QSize mysize = (QSizeF(size()) * dpr).toSize();
108 klfDbg("widget size()="<<size()<<", mysize="<<mysize) ;
109 if (/*labelenabled && */ pGE) {
110 int r = pGEradius * dpr;
111 QSize msz = QSize(2*r, 2*r);
112 if (img.width()+msz.width() > width() || img.height()+msz.height() > height())
113 img = pDisplayImage.scaled(mysize-msz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
114 pix = QPixmap(img.size()+msz);
115 pix.fill(QColor(0,0,0,0));
116 QPainter painter(&pix);
117 painter.translate(QPoint(r, r));
118 klfDrawGlowedImage(&painter, img, pGEcolor, r);
119 } else {
120 if (img.width() > mysize.width() || img.height() > mysize.height()) {
121 img = pDisplayImage.scaled(mysize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
122 }
123 pix = QPixmap::fromImage(img);
124 }
125 pix.setDevicePixelRatio(dpr);
126
127 QPicture labelpic;
128 labelpic.setBoundingRect(QRect(QPoint(0,0), size()));
129 QPainter pp(&labelpic);
130 if (!pLabelEnabled) {
131 pp.setOpacity(0.5f);
132 }
133 QSize pixsizeuser = (QSizeF(pix.size())/dpr).toSize();
134 pp.drawPixmap(QRect(QPoint((width()-pixsizeuser.width())/2, (height()-pixsizeuser.height())/2),
135 pixsizeuser), pix);
136 // // desaturate/grayify the pixmap if we are label-disabled
137 // if (!pLabelEnabled) {
138 // pp.fillRect(QRect(QPoint(0,0), mysize), QColor(255,255,255, 90));
139 // }
140 return labelpic;
141}
142
143void KLFDisplayLabel::display_state(DisplayState state)
144{
145 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
146 pDisplayState = state;
147 if (state == Clear) {
148 setPicture(QPicture());
149 setText(QString());
150 set_error(false);
151 }
152 if (state == Error) {
153 set_error(true);
154 setToolTip(pDisplayError);
155 _bigPreviewText = pDisplayError;
156 }
157 if (state == Ok) {
158 QPicture labelpic = calc_display_picture();
159 setPicture(labelpic);
160
161 // un-set any error
162 set_error(false);
163
164 if (mToolTipFile) {
165 delete mToolTipFile;
166 mToolTipFile = 0;
167 }
168 // no big preview by default
169 _bigPreviewText = "";
170 // but if one is given then prepare it (prepare it even if "enableToolTipPreview" is false,
171 // because we will need it for the "showBigPreview" button)
172 if ( ! pDisplayTooltip.isNull() ) {
173 QString tempdir = QDir::tempPath();
174 mToolTipFile = new QTemporaryFile(tempdir+"/klf_tooltip_XXXXXX.png", this);
175 if ( ! mToolTipFile->open() ) {
176 qWarning("WARNING: Failed open for ToolTip Temp Image!\n%s\n",
177 qPrintable(mToolTipFile->fileTemplate()));
178 delete mToolTipFile;
179 mToolTipFile = 0;
180 } else {
181 mToolTipFile->setAutoRemove(true);
182 bool res = pDisplayTooltip.save(mToolTipFile, "PNG");
183 if ( ! res ) {
184 QMessageBox::critical(this, tr("Error"), tr("Failed write to ToolTip Temp Image file %1!")
185 .arg(mToolTipFile->fileName()));
186 qWarning("WARNING: Failed write to Tooltip temp image to temporary file `%s' !\n",
187 qPrintable(mToolTipFile->fileTemplate()));
188 delete mToolTipFile;
189 mToolTipFile = 0;
190 } else {
191 _bigPreviewText = QString("<img src=\"%1\" width=\"%2\" height=\"%3\" style=\"width:%2px; height:%3px;\">")
192 .arg(mToolTipFile->fileName())
193 .arg((int)(pDisplayTooltip.width() / devicePixelRatioF()))
194 .arg((int)(pDisplayTooltip.height() / devicePixelRatioF()));
195 klfDbg("big preview html = " << _bigPreviewText) ;
196 }
197 }
198 }
199 if (pEnableToolTipPreview) {
200 setToolTip(QString("<p style=\"padding: 8px 8px 8px 8px;\">%1</p>").arg(_bigPreviewText));
201 } else {
202 setToolTip(QString(""));
203 }
204 }
205}
206
207void KLFDisplayLabel::set_error(bool error_on)
208{
209 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
210 setProperty("realTimeLatexError", QVariant(error_on));
211 QPalette *p;
212 if (error_on) {
213 p = &pErrorPalette;
214 } else {
215 p = &pDefaultPalette;
216 }
217 setAutoFillBackground(true);
218 setStyleSheet(styleSheet()); // force style sheet refresh
219 setPalette(*p);
220}
221
222
224{
225 if (pLabelEnabled)
226 emit labelDrag();
227}
virtual void displayClear()
virtual ~KLFDisplayLabel()
virtual void displayError(bool labelenabled=false)
virtual void display(QImage displayimg, QImage tooltipimage, bool labelenabled=true)
KLFDisplayLabel(QWidget *parent)
virtual void mouseMoveEvent(QMouseEvent *e)
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
Definition klfdebug.h:152
#define klfDbg(streamableItems)
print debug stream items
Definition klfdebug.h:158
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.
QString tempPath()
int height() const
bool isNull() const
bool save(const QString &fileName, const char *format, int quality) const
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const
QSize size() const
int width() const
void setColor(ColorGroup group, ColorRole role, const QColor &color)
void setBoundingRect(const QRect &r)
void fill(const QColor &color)
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
void setDevicePixelRatio(qreal scaleFactor)
QSize size() const
int height() const
int width() const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
virtual QString fileName() const
QString fileTemplate() const
void setAutoRemove(bool b)

Generated by doxygen 1.11.0