Go to the documentation of this file.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 <QLabel>
00025 #include <QDir>
00026 #include <QTemporaryFile>
00027 #include <QMessageBox>
00028 #include <QVariant>
00029 #include <QPainter>
00030
00031 #include <klfguiutil.h>
00032 #include "klfdisplaylabel.h"
00033
00034
00035 KLFDisplayLabel::KLFDisplayLabel(QWidget *parent)
00036 : QLabel(parent), pEnableToolTipPreview(true), mToolTipFile(NULL)
00037 {
00038 setText(QString());
00039
00040
00041 setAlignment(Qt::AlignCenter);
00042
00043
00044
00045
00046 pDefaultPalette = palette();
00047 pErrorPalette = pDefaultPalette;
00048
00049 pDefaultPalette.setColor(QPalette::Window, QColor(255, 255, 255, 0));
00050 pErrorPalette.setColor(QPalette::Window, QColor(255, 0, 0, 60));
00051
00052 pGE = false;
00053 pGEcolor = QColor(128, 255, 128, 8);
00054 pGEradius = 4;
00055 }
00056
00057 KLFDisplayLabel::~KLFDisplayLabel()
00058 {
00059 if (mToolTipFile)
00060 delete mToolTipFile;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 void KLFDisplayLabel::displayClear()
00073 {
00074 display_state(Clear);
00075
00076 pLabelEnabled = false;
00077 }
00078
00079 void KLFDisplayLabel::display(QImage displayimg, QImage tooltipimage, bool labelenabled)
00080 {
00081 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00082
00083 pDisplayImage = displayimg;
00084 pDisplayTooltip = tooltipimage;
00085
00086 pLabelEnabled = labelenabled;
00087 display_state(Ok);
00088 }
00089
00090 void KLFDisplayLabel::displayError(const QString& errorMessage, bool labelenabled)
00091 {
00092 pDisplayError = errorMessage;
00093
00094 pLabelEnabled = labelenabled;
00095 display_state(Error);
00096 }
00097
00098
00099 QPicture KLFDisplayLabel::calc_display_picture()
00100 {
00101 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00102
00103 double dpr = devicePixelRatioF();
00104
00105 QImage img = pDisplayImage;
00106 QPixmap pix;
00107 QSize mysize = (QSizeF(size()) * dpr).toSize();
00108 klfDbg("widget size()="<<size()<<", mysize="<<mysize) ;
00109 if ( pGE) {
00110 int r = pGEradius * dpr;
00111 QSize msz = QSize(2*r, 2*r);
00112 if (img.width()+msz.width() > width() || img.height()+msz.height() > height())
00113 img = pDisplayImage.scaled(mysize-msz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
00114 pix = QPixmap(img.size()+msz);
00115 pix.fill(QColor(0,0,0,0));
00116 QPainter painter(&pix);
00117 painter.translate(QPoint(r, r));
00118 klfDrawGlowedImage(&painter, img, pGEcolor, r);
00119 } else {
00120 if (img.width() > mysize.width() || img.height() > mysize.height()) {
00121 img = pDisplayImage.scaled(mysize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
00122 }
00123 pix = QPixmap::fromImage(img);
00124 }
00125 pix.setDevicePixelRatio(dpr);
00126
00127 QPicture labelpic;
00128 labelpic.setBoundingRect(QRect(QPoint(0,0), size()));
00129 QPainter pp(&labelpic);
00130 if (!pLabelEnabled) {
00131 pp.setOpacity(0.5f);
00132 }
00133 QSize pixsizeuser = (QSizeF(pix.size())/dpr).toSize();
00134 pp.drawPixmap(QRect(QPoint((width()-pixsizeuser.width())/2, (height()-pixsizeuser.height())/2),
00135 pixsizeuser), pix);
00136
00137
00138
00139
00140 return labelpic;
00141 }
00142
00143 void KLFDisplayLabel::display_state(DisplayState state)
00144 {
00145 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00146 pDisplayState = state;
00147 if (state == Clear) {
00148 setPicture(QPicture());
00149 setText(QString());
00150 set_error(false);
00151 }
00152 if (state == Error) {
00153 set_error(true);
00154 setToolTip(pDisplayError);
00155 _bigPreviewText = pDisplayError;
00156 }
00157 if (state == Ok) {
00158 QPicture labelpic = calc_display_picture();
00159 setPicture(labelpic);
00160
00161
00162 set_error(false);
00163
00164 if (mToolTipFile) {
00165 delete mToolTipFile;
00166 mToolTipFile = 0;
00167 }
00168
00169 _bigPreviewText = "";
00170
00171
00172 if ( ! pDisplayTooltip.isNull() ) {
00173 QString tempdir = QDir::tempPath();
00174 mToolTipFile = new QTemporaryFile(tempdir+"/klf_tooltip_XXXXXX.png", this);
00175 if ( ! mToolTipFile->open() ) {
00176 qWarning("WARNING: Failed open for ToolTip Temp Image!\n%s\n",
00177 qPrintable(mToolTipFile->fileTemplate()));
00178 delete mToolTipFile;
00179 mToolTipFile = 0;
00180 } else {
00181 mToolTipFile->setAutoRemove(true);
00182 bool res = pDisplayTooltip.save(mToolTipFile, "PNG");
00183 if ( ! res ) {
00184 QMessageBox::critical(this, tr("Error"), tr("Failed write to ToolTip Temp Image file %1!")
00185 .arg(mToolTipFile->fileName()));
00186 qWarning("WARNING: Failed write to Tooltip temp image to temporary file `%s' !\n",
00187 qPrintable(mToolTipFile->fileTemplate()));
00188 delete mToolTipFile;
00189 mToolTipFile = 0;
00190 } else {
00191 _bigPreviewText = QString("<img src=\"%1\" width=\"%2\" height=\"%3\" style=\"width:%2px; height:%3px;\">")
00192 .arg(mToolTipFile->fileName())
00193 .arg((int)(pDisplayTooltip.width() / devicePixelRatioF()))
00194 .arg((int)(pDisplayTooltip.height() / devicePixelRatioF()));
00195 klfDbg("big preview html = " << _bigPreviewText) ;
00196 }
00197 }
00198 }
00199 if (pEnableToolTipPreview) {
00200 setToolTip(QString("<p style=\"padding: 8px 8px 8px 8px;\">%1</p>").arg(_bigPreviewText));
00201 } else {
00202 setToolTip(QString(""));
00203 }
00204 }
00205 }
00206
00207 void KLFDisplayLabel::set_error(bool error_on)
00208 {
00209 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00210 setProperty("realTimeLatexError", QVariant(error_on));
00211 QPalette *p;
00212 if (error_on) {
00213 p = &pErrorPalette;
00214 } else {
00215 p = &pDefaultPalette;
00216 }
00217 setAutoFillBackground(true);
00218 setStyleSheet(styleSheet());
00219 setPalette(*p);
00220 }
00221
00222
00223 void KLFDisplayLabel::mouseMoveEvent(QMouseEvent *)
00224 {
00225 if (pLabelEnabled)
00226 emit labelDrag();
00227 }