[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfdisplaylabel.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfdisplaylabel.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
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   //  setLabelFixedSize(QSize(120,80));
00040 
00041   setAlignment(Qt::AlignCenter);
00042 
00043   //don't set this to true, because otherwise resizing the label distorts the image
00044   //setScaledContents(true);
00045 
00046   pDefaultPalette = palette();
00047   pErrorPalette = pDefaultPalette;
00048 
00049   pDefaultPalette.setColor(QPalette::Window, QColor(255, 255, 255, 0)); // fully transparent
00050   pErrorPalette.setColor(QPalette::Window, QColor(255, 0, 0, 60)); // red color, semi-transparent
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 void KLFDisplayLabel::setLabelFixedSize(const QSize& size)
00065 {
00066   pLabelFixedSize = size;
00067   setMinimumSize(size);
00068   setFixedSize(size);
00069 }
00070 */
00071 
00072 void KLFDisplayLabel::displayClear()
00073 {
00074   display_state(Clear);
00075   //  setEnabled(false);
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 (/*labelenabled && */ 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   //  // desaturate/grayify the pixmap if we are label-disabled
00137   //  if (!pLabelEnabled) {
00138   //    pp.fillRect(QRect(QPoint(0,0), mysize), QColor(255,255,255, 90));
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     // un-set any error
00162     set_error(false);
00163 
00164     if (mToolTipFile) {
00165       delete mToolTipFile;
00166       mToolTipFile = 0;
00167     }
00168     // no big preview by default
00169     _bigPreviewText = "";
00170     // but if one is given then prepare it (prepare it even if "enableToolTipPreview" is false,
00171     // because we will need it for the "showBigPreview" button)
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()); // force style sheet refresh
00219   setPalette(*p);
00220 }
00221 
00222 
00223 void KLFDisplayLabel::mouseMoveEvent(QMouseEvent */*e*/)
00224 {
00225   if (pLabelEnabled)
00226     emit labelDrag();
00227 }

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net