[KLF Application][KLF Tools][KLF Backend][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) 2009 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: klfdisplaylabel.cpp 507 2010-09-22 02:04:08Z philippe $ */
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   pDefaultPalette = palette();
00044   pErrorPalette = pDefaultPalette;
00045   pErrorPalette.setColor(QPalette::Window, QColor(255, 200, 200));
00046 
00047   pGE = false;
00048   pGEcolor = QColor(128, 255, 128, 8);
00049   pGEradius = 4;
00050 }
00051 
00052 KLFDisplayLabel::~KLFDisplayLabel()
00053 {
00054   if (mToolTipFile)
00055     delete mToolTipFile;
00056 }
00057 
00058 void KLFDisplayLabel::setLabelFixedSize(const QSize& size)
00059 {
00060   pLabelFixedSize = size;
00061   setMinimumSize(size);
00062   setFixedSize(size);
00063 }
00064 
00065 
00066 void KLFDisplayLabel::displayClear()
00067 {
00068   setPixmap(QPixmap());
00069   setText(QString());
00070   setEnabled(false);
00071   set_error(false);
00072 }
00073 
00074 void KLFDisplayLabel::display(QImage displayimg, QImage tooltipimage, bool labelenabled)
00075 {
00076   QImage img = displayimg;
00077   QPixmap pix;
00078   if (labelenabled && pGE) {
00079     int r = pGEradius;
00080     QSize msz = QSize(2*r, 2*r);
00081     if (img.width()+msz.width() > width() || img.height()+msz.height() > height())
00082       img = displayimg.scaled(size()-msz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
00083     pix = QPixmap(img.size()+msz);
00084     pix.fill(QColor(0,0,0,0));
00085     QPainter painter(&pix);
00086     painter.translate(QPoint(r, r));
00087     klfDrawGlowedImage(&painter, img, pGEcolor, r);
00088   } else {
00089     if (img.width() > width() || img.height() > height())
00090       img = displayimg.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
00091     pix = QPixmap::fromImage(img);
00092   }
00093   setPixmap(pix);
00094 
00095   // un-set any error
00096   set_error(false);
00097 
00098   if (mToolTipFile) {
00099     delete mToolTipFile;
00100     mToolTipFile = 0;
00101   }
00102   // no big preview by default
00103   _bigPreviewText = "";
00104   // but if one is given then prepare it (prepare it even if "enableToolTipPreview" is false,
00105   // because we will need it for the "showBigPreview" button)
00106   if ( ! tooltipimage.isNull() ) {
00107     QString tempdir = QDir::tempPath();
00108     mToolTipFile = new QTemporaryFile(tempdir+"/klf_tooltip_XXXXXX.png", this);
00109     if ( ! mToolTipFile->open() ) {
00110       qWarning("WARNING: Failed open for ToolTip Temp Image!\n%s\n",
00111                qPrintable(mToolTipFile->fileTemplate()));
00112       delete mToolTipFile;
00113       mToolTipFile = 0;
00114     } else {
00115       mToolTipFile->setAutoRemove(true);
00116       bool res = tooltipimage.save(mToolTipFile, "PNG");
00117       if ( ! res ) {
00118         QMessageBox::critical(this, tr("Error"), tr("Failed write to ToolTip Temp Image file %1!")
00119                               .arg(mToolTipFile->fileName()));
00120         qWarning("WARNING: Failed write to Tooltip temp image to temporary file `%s' !\n",
00121                  qPrintable(mToolTipFile->fileTemplate()));
00122         delete mToolTipFile;
00123         mToolTipFile = 0;
00124       } else {
00125         _bigPreviewText = QString("<img src=\"%1\">").arg(mToolTipFile->fileName());
00126       }
00127     }
00128   }
00129   if (pEnableToolTipPreview) {
00130     setToolTip(QString("<p style=\"padding: 8px 8px 8px 8px;\">%1</p>").arg(_bigPreviewText));
00131   } else {
00132     setToolTip(QString(""));
00133   }
00134 
00135   setEnabled(labelenabled);
00136 }
00137 
00138 void KLFDisplayLabel::displayError(bool labelenabled)
00139 {
00140   set_error(true);
00141   setEnabled(labelenabled);
00142 }
00143 
00144 
00145 void KLFDisplayLabel::set_error(bool error_on)
00146 {
00147   setProperty("realTimeLatexError", QVariant(error_on));
00148   QPalette *p;
00149   if (error_on) {
00150     p = &pErrorPalette;
00151   } else {
00152     p = &pDefaultPalette;
00153   }
00154   setAutoFillBackground(true);
00155   setStyleSheet(styleSheet()); // force style sheet refresh
00156   setPalette(*p);
00157 }
00158 
00159 void KLFDisplayLabel::mouseMoveEvent(QMouseEvent */*e*/)
00160 {
00161   emit labelDrag();
00162 }

Generated by doxygen 1.7.3