[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klflibentryeditor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klflibentryeditor.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2010 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: klflibentryeditor.cpp 483 2010-09-10 00:43:05Z philippe $ */
00023 
00024 
00025 #include <QWidget>
00026 #include <QComboBox>
00027 #include <QLineEdit>
00028 #include <QEvent>
00029 #include <QKeyEvent>
00030 #include <QPixmap>
00031 
00032 #include <klfdisplaylabel.h>
00033 
00034 #include "klfconfig.h"
00035 #include "klflib.h"
00036 #include "klflatexedit.h"
00037 
00038 #include <ui_klflibentryeditor.h>
00039 #include "klflibentryeditor.h"
00040 
00041 
00042 
00043 
00044 
00045 KLFLibEntryEditor::KLFLibEntryEditor(QWidget *parent)
00046   : QWidget(parent), pInputEnabled(true)
00047 {
00048   u = new Ui::KLFLibEntryEditor;
00049   u->setupUi(this);
00050   setAutoFillBackground(false);
00051 
00052   pCurrentStyle = KLFStyle();
00053 
00054   u->lblPreview->setLabelFixedSize(klfconfig.UI.labelOutputFixedSize);
00055 
00056   u->cbxCategory->setInsertPolicy(QComboBox::InsertAlphabetically);
00057   u->cbxCategory->setDuplicatesEnabled(false);
00058   u->cbxTags->setInsertPolicy(QComboBox::InsertAlphabetically);
00059   u->cbxTags->setDuplicatesEnabled(false);
00060 
00061   u->cbxCategory->installEventFilter(this);
00062   u->cbxTags->installEventFilter(this);
00063 
00064   u->cbxCategory->addItem("");
00065   u->cbxTags->addItem("");
00066 
00067   // do NOT automatically apply changes, rather enable the 'apply changes' button
00068   //  connect(u->cbxCategory, SIGNAL(activated(int)), this, SLOT(slotApplyChanges()));
00069   //  connect(u->cbxTags, SIGNAL(activated(int)), this, SLOT(slotApplyChanges()));
00070   connect(u->cbxCategory, SIGNAL(activated(int)), this, SLOT(slotModified()));
00071   connect(u->cbxCategory, SIGNAL(editTextChanged(const QString&)), this, SLOT(slotModified()));
00072   connect(u->cbxTags, SIGNAL(activated(int)), this, SLOT(slotModified()));
00073   connect(u->cbxTags, SIGNAL(editTextChanged(const QString&)), this, SLOT(slotModified()));
00074 
00075   // preview and latexpreview should be as small as possible (while
00076   // still respecting their minimum sizes...)
00077   u->splitEntryEditor->setSizes(QList<int>() << 100 << 1000);
00078 
00079   // setup latex preview / preamble preview text browser
00080   u->txtPreviewLatex->setFont(klfconfig.UI.preambleEditFont);
00081   u->txtStyPreamble->setFont(klfconfig.UI.preambleEditFont);
00082   u->txtStyPreamble->setHeightHintLines(4);
00083 }
00084 void KLFLibEntryEditor::retranslateUi(bool alsoBaseUi)
00085 {
00086   if (alsoBaseUi)
00087     u->retranslateUi(this);
00088 }
00089 KLFLibEntryEditor::~KLFLibEntryEditor()
00090 {
00091   delete u;
00092 }
00093 
00094 bool KLFLibEntryEditor::eventFilter(QObject *object, QEvent *event)
00095 {
00096   if (object == u->cbxCategory || object == u->cbxTags) {
00097     QComboBox *cbx = qobject_cast<QComboBox*>(object);
00098     if (event->type() == QEvent::KeyPress) {
00099       QKeyEvent *ke = (QKeyEvent*) event;
00100       if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) {
00101         slotUpdateFromCbx(cbx);
00102         return true;
00103       }
00104     }
00105   }
00106 
00107   return QWidget::eventFilter(object, event);
00108 }
00109 
00110 void KLFLibEntryEditor::addCategorySuggestions(const QStringList& categorylist)
00111 {
00112   u->cbxCategory->addItems(categorylist);
00113   slotCbxCleanUpCompletions(u->cbxCategory);
00114 }
00115 
00116 
00117 void KLFLibEntryEditor::displayEntry(const KLFLibEntry& entry)
00118 {
00119   displayEntries(QList<KLFLibEntry>() << entry);
00120 }
00121 
00122 void KLFLibEntryEditor::displayEntries(const QList<KLFLibEntry>& entrylist)
00123 {
00124   // refresh the display label's glow effect
00126   u->lblPreview->setGlowEffect(klfconfig.UI.glowEffect);
00127   u->lblPreview->setGlowEffectColor(klfconfig.UI.glowEffectColor);
00128   u->lblPreview->setGlowEffectRadius(klfconfig.UI.glowEffectRadius);
00129 
00130   u->cbxCategory->lineEdit()->setReadOnly(!pInputEnabled);
00131   u->cbxTags->lineEdit()->setReadOnly(!pInputEnabled);
00132   if (entrylist.size() == 0) {
00133     u->lblPreview->display(QImage(":/pics/nopreview.png"), QImage(), false);
00134     u->txtPreviewLatex->setText(tr("[ No Item Selected ]"));
00135     u->cbxCategory->setEditText(tr("[ No Item Selected ]"));
00136     u->cbxTags->setEditText(tr("[ No Item Selected ]"));
00137     //    u->lblStylePreview->setText(tr("[ No Item Selected ]"));
00138     u->cbxCategory->setEnabled(false);
00139     u->cbxTags->setEnabled(false);
00140     u->btnApplyChanges->setEnabled(false);
00141     u->btnRestoreStyle->setEnabled(false);
00142     pCurrentStyle = KLFStyle();
00143     displayStyle(false, KLFStyle());
00144     u->lblStyMathMode->setText(tr("[ No Item Selected ]"));
00145     u->txtStyPreamble->setPlainText(tr("[ No Item Selected ]"));
00146     slotModified(false);
00147     return;
00148   }
00149   if (entrylist.size() == 1) {
00150     KLFLibEntry e = entrylist[0];
00151     QImage img = e.preview();
00152     u->lblPreview->display(img, img, true);
00153     u->txtPreviewLatex->setText(e.latex());
00154     u->cbxCategory->setEditText(e.category());
00155     u->cbxTags->setEditText(e.tags());
00156     pCurrentStyle = e.style();
00157     //    u->lblStylePreview->setText(prettyPrintStyle(pCurrentStyle));
00158     u->cbxCategory->setEnabled(true);
00159     u->cbxTags->setEnabled(true);
00160     u->btnApplyChanges->setEnabled(pInputEnabled && true);
00161     u->btnRestoreStyle->setEnabled(true); // NOT pInputEnabled && : not true input
00162     displayStyle(true, pCurrentStyle);
00163     slotModified(false);
00164     return;
00165   }
00166   // multiple items selected
00167   u->lblPreview->display(QImage(":/pics/nopreview.png"), QImage(), false);
00168   u->txtPreviewLatex->setText(tr("[ %n Items Selected ]", 0, entrylist.size()));
00169   u->cbxTags->setEditText(tr("[ Multiple Items Selected ]"));
00170   // if all elements have same category and style, display them, otherwise set
00171   // the respective field empty
00172   QString cat;
00173   bool allsamestyle = true;
00174   KLFStyle style;
00175   int k;
00176   for (k = 0; k < entrylist.size(); ++k) {
00177     QString thiscat = entrylist[k].category();
00178     KLFStyle thisstyle = entrylist[k].style();
00179     if (k == 0) {
00180       cat = thiscat;
00181       style = thisstyle;
00182       allsamestyle = true;
00183       continue;
00184     }
00185     if ( !cat.isEmpty() && thiscat != cat ) {
00186       cat = "";
00187     }
00188     if ( allsamestyle && !(style == thisstyle) ) {
00189       allsamestyle = false;
00190     }
00191   }
00192   u->cbxCategory->setEditText(cat);
00193   if ( allsamestyle ) {
00194     pCurrentStyle = style;
00195     displayStyle(true, pCurrentStyle);
00196     u->btnRestoreStyle->setEnabled(true); // NOT pInputEnabled && : not true input
00197   } else {
00198     pCurrentStyle = KLFStyle();
00199     displayStyle(false, KLFStyle());
00200     u->lblStyMathMode->setText(tr("[ Different Styles ]"));
00201     u->txtStyPreamble->setPlainText(tr("[ Different Styles ]"));
00202     u->btnRestoreStyle->setEnabled(false);
00203   }
00204 
00205   u->cbxCategory->setEnabled(pInputEnabled && true);
00206   u->cbxTags->setEnabled(pInputEnabled && false);
00207   u->btnApplyChanges->setEnabled(pInputEnabled && true);
00208   slotModified(false);
00209 }
00210 
00211 // private
00212 void KLFLibEntryEditor::displayStyle(bool valid, const KLFStyle& style)
00213 {
00214   if (valid) {
00215     u->lblStyDPI->setText(QString::number(style.dpi));
00216     QPixmap pxfg(16, 16);
00217     pxfg.fill(QColor(style.fg_color));
00218     u->lblStyColFg->setPixmap(pxfg);
00219     if (qAlpha(style.bg_color)) {
00220       QPixmap pxbg(16, 16);
00221       pxbg.fill(QColor(style.bg_color));
00222       u->lblStyColBg->setPixmap(pxbg);
00223     } else {
00224       u->lblStyColBg->setPixmap(QPixmap(":pics/transparenticon16.png"));
00225     }
00226     u->lblStyMathMode->setText(style.mathmode);
00227     u->txtStyPreamble->setPlainText(style.preamble);
00228   } else {
00229     u->lblStyDPI->setText(QLatin1String("-"));
00230     u->lblStyColFg->setText(QString());
00231     u->lblStyColFg->setPixmap(QPixmap());
00232     u->lblStyColBg->setText(QString());
00233     u->lblStyColBg->setPixmap(QPixmap());
00234     u->lblStyMathMode->setText(QString());
00235     u->txtStyPreamble->setPlainText(QString());
00236   }
00237 }
00238 
00239 
00240 void KLFLibEntryEditor::setInputEnabled(bool enabled)
00241 {
00242   pInputEnabled = enabled;
00243 }
00244 
00245 void KLFLibEntryEditor::slotModified(bool modif)
00246 {
00247   pMetaInfoModified = modif;
00248   u->btnApplyChanges->setEnabled(pMetaInfoModified);
00249 }
00250 
00251 
00252 void KLFLibEntryEditor::slotUpdateFromCbx(QComboBox *cbx)
00253 {
00254   // Apply all changes, this is more natural than applying only the changes
00255   // to the current cbx (and losing the changes to the other)
00256   if (cbx == u->cbxCategory)
00257     //    slotApplyChanges(true, false);
00258     slotApplyChanges();
00259   else if (cbx == u->cbxTags)
00260     //    slotApplyChanges(false, true);
00261     slotApplyChanges();
00262   else
00263     qWarning("KLFLibEntryEditor::slotUpdateFromCbx: Couldn't find combo box=%p", (void*)cbx);
00264 }
00265 
00266 void KLFLibEntryEditor::on_btnApplyChanges_clicked()
00267 {
00268   slotApplyChanges(u->cbxCategory->isEnabled(), u->cbxTags->isEnabled());
00269 }
00270 void KLFLibEntryEditor::slotApplyChanges(bool cat, bool tags)
00271 {
00272   klfDbg("category="<<cat<<" tags="<<tags) ;
00273   QMap<int,QVariant> data;
00274   if (cat && u->cbxCategory->isEnabled()) {
00275     slotCbxSaveCurrentCompletion(u->cbxCategory);
00276     data[KLFLibEntry::Category] = u->cbxCategory->currentText();
00277   }
00278   if (tags && u->cbxTags->isEnabled()) {
00279     slotCbxSaveCurrentCompletion(u->cbxTags);
00280     data[KLFLibEntry::Tags] = u->cbxTags->currentText();
00281   }
00282   klfDbg("data to update: "<<data) ;
00283   if (data.isEmpty())
00284     return;
00285 
00286   emit metaInfoChanged(data);
00287 }
00288 
00289 void KLFLibEntryEditor::on_btnRestoreStyle_clicked()
00290 {
00291   emit restoreStyle(pCurrentStyle);
00292 }
00293 
00294 void KLFLibEntryEditor::slotCbxSaveCurrentCompletion(QComboBox *cbx)
00295 {
00296   cbx->addItem(cbx->currentText());
00297   slotCbxCleanUpCompletions(cbx);
00298 }
00299 
00300 void KLFLibEntryEditor::slotCbxCleanUpCompletions(QComboBox *cbx)
00301 {
00302   cbx->blockSignals(true);
00303   QString bkp_edittext = cbx->currentText();
00304 
00305   QStringList items;
00306   QStringList uitems;
00307   int k;
00308   for (k = 0; k < cbx->count(); ++k) {
00309     items << cbx->itemText(k);
00310   }
00311   items.sort();
00312   // unique items now
00313   for (k = 0; k < items.size(); ++k) {
00314     if ( ! uitems.contains(items[k]) )
00315       uitems << items[k];
00316   }
00317   // remove all items
00318   while (cbx->count())
00319     cbx->removeItem(0);
00320   cbx->addItems(uitems);
00321 
00322   cbx->setEditText(bkp_edittext);
00323   cbx->blockSignals(false);
00324 }

Generated by doxygen 1.7.3