[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klflibentryeditor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klflibentryeditor.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: klflibentryeditor.cpp 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 
25 #include <QWidget>
26 #include <QComboBox>
27 #include <QLineEdit>
28 #include <QEvent>
29 #include <QKeyEvent>
30 #include <QPixmap>
31 
32 #include <klfdisplaylabel.h>
33 
34 #include "klfconfig.h"
35 #include "klflib.h"
36 #include "klflatexedit.h"
37 
38 #include <ui_klflibentryeditor.h>
39 #include "klflibentryeditor.h"
40 
41 
42 
43 
44 
46  : QWidget(parent), pInputEnabled(true)
47 {
48  u = new Ui::KLFLibEntryEditor;
49  u->setupUi(this);
50  setAutoFillBackground(false);
51 
52  pCurrentStyle = KLFStyle();
53 
54  u->lblPreview->setLabelFixedSize(klfconfig.UI.labelOutputFixedSize);
55 
56  u->cbxCategory->setInsertPolicy(QComboBox::InsertAlphabetically);
57  u->cbxCategory->setDuplicatesEnabled(false);
58  u->cbxTags->setInsertPolicy(QComboBox::InsertAlphabetically);
59  u->cbxTags->setDuplicatesEnabled(false);
60 
61  u->cbxCategory->installEventFilter(this);
62  u->cbxTags->installEventFilter(this);
63 
64  u->cbxCategory->addItem("");
65  u->cbxTags->addItem("");
66 
67  // do NOT automatically apply changes, rather enable the 'apply changes' button
68  // connect(u->cbxCategory, SIGNAL(activated(int)), this, SLOT(slotApplyChanges()));
69  // connect(u->cbxTags, SIGNAL(activated(int)), this, SLOT(slotApplyChanges()));
70  connect(u->cbxCategory, SIGNAL(activated(int)), this, SLOT(slotModified()));
71  connect(u->cbxCategory, SIGNAL(editTextChanged(const QString&)), this, SLOT(slotModified()));
72  connect(u->cbxTags, SIGNAL(activated(int)), this, SLOT(slotModified()));
73  connect(u->cbxTags, SIGNAL(editTextChanged(const QString&)), this, SLOT(slotModified()));
74 
75  // preview and latexpreview should be as small as possible (while
76  // still respecting their minimum sizes...)
77  u->splitEntryEditor->setSizes(QList<int>() << 100 << 1000);
78 
79  // setup latex preview / preamble preview text browser
80  u->txtPreviewLatex->setFont(klfconfig.UI.preambleEditFont);
81  u->txtStyPreamble->setFont(klfconfig.UI.preambleEditFont);
82  u->txtStyPreamble->setHeightHintLines(4);
83 }
84 void KLFLibEntryEditor::retranslateUi(bool alsoBaseUi)
85 {
86  if (alsoBaseUi)
87  u->retranslateUi(this);
88 }
90 {
91  delete u;
92 }
93 
95 {
96  if (object == u->cbxCategory || object == u->cbxTags) {
97  QComboBox *cbx = qobject_cast<QComboBox*>(object);
98  if (event->type() == QEvent::KeyPress) {
99  QKeyEvent *ke = (QKeyEvent*) event;
100  if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) {
101  slotUpdateFromCbx(cbx);
102  return true;
103  }
104  }
105  }
106 
107  return QWidget::eventFilter(object, event);
108 }
109 
111 {
112  u->cbxCategory->addItems(categorylist);
113  slotCbxCleanUpCompletions(u->cbxCategory);
114 }
115 
116 
118 {
120 }
121 
123 {
124  // refresh the display label's glow effect
126  u->lblPreview->setGlowEffect(klfconfig.UI.glowEffect);
127  u->lblPreview->setGlowEffectColor(klfconfig.UI.glowEffectColor);
128  u->lblPreview->setGlowEffectRadius(klfconfig.UI.glowEffectRadius);
129 
130  u->cbxCategory->lineEdit()->setReadOnly(!pInputEnabled);
131  u->cbxTags->lineEdit()->setReadOnly(!pInputEnabled);
132  if (entrylist.size() == 0) {
133  u->lblPreview->display(QImage(":/pics/nopreview.png"), QImage(), false);
134  u->txtPreviewLatex->setText(tr("[ No Item Selected ]"));
135  u->cbxCategory->setEditText(tr("[ No Item Selected ]"));
136  u->cbxTags->setEditText(tr("[ No Item Selected ]"));
137  // u->lblStylePreview->setText(tr("[ No Item Selected ]"));
138  u->cbxCategory->setEnabled(false);
139  u->cbxTags->setEnabled(false);
140  u->btnApplyChanges->setEnabled(false);
141  u->btnRestoreStyle->setEnabled(false);
142  pCurrentStyle = KLFStyle();
143  displayStyle(false, KLFStyle());
144  u->lblStyMathMode->setText(tr("[ No Item Selected ]"));
145  u->txtStyPreamble->setPlainText(tr("[ No Item Selected ]"));
146  slotModified(false);
147  return;
148  }
149  if (entrylist.size() == 1) {
150  KLFLibEntry e = entrylist[0];
151  QImage img = e.preview();
152  u->lblPreview->display(img, img, true);
153  u->txtPreviewLatex->setText(e.latex());
154  u->cbxCategory->setEditText(e.category());
155  u->cbxTags->setEditText(e.tags());
156  pCurrentStyle = e.style();
157  // u->lblStylePreview->setText(prettyPrintStyle(pCurrentStyle));
158  u->cbxCategory->setEnabled(true);
159  u->cbxTags->setEnabled(true);
160  u->btnApplyChanges->setEnabled(pInputEnabled && true);
161  u->btnRestoreStyle->setEnabled(true); // NOT pInputEnabled && : not true input
162  displayStyle(true, pCurrentStyle);
163  slotModified(false);
164  return;
165  }
166  // multiple items selected
167  u->lblPreview->display(QImage(":/pics/nopreview.png"), QImage(), false);
168  u->txtPreviewLatex->setText(tr("[ %n Items Selected ]", 0, entrylist.size()));
169  u->cbxTags->setEditText(tr("[ Multiple Items Selected ]"));
170  // if all elements have same category and style, display them, otherwise set
171  // the respective field empty
172  QString cat;
173  bool allsamestyle = true;
174  KLFStyle style;
175  int k;
176  for (k = 0; k < entrylist.size(); ++k) {
177  QString thiscat = entrylist[k].category();
178  KLFStyle thisstyle = entrylist[k].style();
179  if (k == 0) {
180  cat = thiscat;
181  style = thisstyle;
182  allsamestyle = true;
183  continue;
184  }
185  if ( !cat.isEmpty() && thiscat != cat ) {
186  cat = "";
187  }
188  if ( allsamestyle && !(style == thisstyle) ) {
189  allsamestyle = false;
190  }
191  }
192  u->cbxCategory->setEditText(cat);
193  if ( allsamestyle ) {
194  pCurrentStyle = style;
195  displayStyle(true, pCurrentStyle);
196  u->btnRestoreStyle->setEnabled(true); // NOT pInputEnabled && : not true input
197  } else {
198  pCurrentStyle = KLFStyle();
199  displayStyle(false, KLFStyle());
200  u->lblStyMathMode->setText(tr("[ Different Styles ]"));
201  u->txtStyPreamble->setPlainText(tr("[ Different Styles ]"));
202  u->btnRestoreStyle->setEnabled(false);
203  }
204 
205  u->cbxCategory->setEnabled(pInputEnabled && true);
206  u->cbxTags->setEnabled(pInputEnabled && false);
207  u->btnApplyChanges->setEnabled(pInputEnabled && true);
208  slotModified(false);
209 }
210 
211 // private
212 void KLFLibEntryEditor::displayStyle(bool valid, const KLFStyle& style)
213 {
214  if (valid) {
215  u->lblStyDPI->setText(QString::number(style.dpi));
216  QPixmap pxfg(16, 16);
217  pxfg.fill(QColor(style.fg_color));
218  u->lblStyColFg->setPixmap(pxfg);
219  if (qAlpha(style.bg_color)) {
220  QPixmap pxbg(16, 16);
221  pxbg.fill(QColor(style.bg_color));
222  u->lblStyColBg->setPixmap(pxbg);
223  } else {
224  u->lblStyColBg->setPixmap(QPixmap(":pics/transparenticon16.png"));
225  }
226  u->lblStyMathMode->setText(style.mathmode);
227  u->txtStyPreamble->setPlainText(style.preamble);
228  } else {
229  u->lblStyDPI->setText(QLatin1String("-"));
230  u->lblStyColFg->setText(QString());
231  u->lblStyColFg->setPixmap(QPixmap());
232  u->lblStyColBg->setText(QString());
233  u->lblStyColBg->setPixmap(QPixmap());
234  u->lblStyMathMode->setText(QString());
235  u->txtStyPreamble->setPlainText(QString());
236  }
237 }
238 
239 
241 {
242  pInputEnabled = enabled;
243 }
244 
246 {
247  pMetaInfoModified = modif;
248  u->btnApplyChanges->setEnabled(pMetaInfoModified);
249 }
250 
251 
253 {
254  // Apply all changes, this is more natural than applying only the changes
255  // to the current cbx (and losing the changes to the other)
256  if (cbx == u->cbxCategory)
257  // slotApplyChanges(true, false);
259  else if (cbx == u->cbxTags)
260  // slotApplyChanges(false, true);
262  else
263  qWarning("KLFLibEntryEditor::slotUpdateFromCbx: Couldn't find combo box=%p", (void*)cbx);
264 }
265 
267 {
268  slotApplyChanges(u->cbxCategory->isEnabled(), u->cbxTags->isEnabled());
269 }
270 void KLFLibEntryEditor::slotApplyChanges(bool cat, bool tags)
271 {
272  klfDbg("category="<<cat<<" tags="<<tags) ;
273  QMap<int,QVariant> data;
274  if (cat && u->cbxCategory->isEnabled()) {
275  slotCbxSaveCurrentCompletion(u->cbxCategory);
276  data[KLFLibEntry::Category] = u->cbxCategory->currentText();
277  }
278  if (tags && u->cbxTags->isEnabled()) {
279  slotCbxSaveCurrentCompletion(u->cbxTags);
280  data[KLFLibEntry::Tags] = u->cbxTags->currentText();
281  }
282  klfDbg("data to update: "<<data) ;
283  if (data.isEmpty())
284  return;
285 
286  emit metaInfoChanged(data);
287 }
288 
290 {
291  emit restoreStyle(pCurrentStyle);
292 }
293 
295 {
296  cbx->addItem(cbx->currentText());
298 }
299 
301 {
302  cbx->blockSignals(true);
303  QString bkp_edittext = cbx->currentText();
304 
305  QStringList items;
306  QStringList uitems;
307  int k;
308  for (k = 0; k < cbx->count(); ++k) {
309  items << cbx->itemText(k);
310  }
311  items.sort();
312  // unique items now
313  for (k = 0; k < items.size(); ++k) {
314  if ( ! uitems.contains(items[k]) )
315  uitems << items[k];
316  }
317  // remove all items
318  while (cbx->count())
319  cbx->removeItem(0);
320  cbx->addItems(uitems);
321 
322  cbx->setEditText(bkp_edittext);
323  cbx->blockSignals(false);
324 }
void addCategorySuggestions(const QStringList &categorylist)
virtual bool eventFilter(QObject *object, QEvent *event)
KLFConfig klfconfig
Definition: klfconfig.cpp:88
bool glowEffect
Definition: klfconfig.h:197
struct KLFConfig::@1 UI
contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive)
#define klfDbg(streamableItems)
QColor glowEffectColor
Definition: klfconfig.h:198
void slotModified(bool modif=true)
setEditText(const QString &text)
itemText(int index)
QFont preambleEditFont
Definition: klfconfig.h:181
void restoreStyle(const KLFStyle &style)
QString mathmode
QString latex() const
Definition: klflib.h:82
void slotCbxSaveCurrentCompletion(QComboBox *cbx)
void slotCbxCleanUpCompletions(QComboBox *cbx)
addItem(const QString &text, const QVariant &userData=QVariant()
number(long n, int base=10)
KLFLibEntryEditor(QWidget *parent=NULL)
void metaInfoChanged(const QMap< int, QVariant > &props)
void setInputEnabled(bool enabled)
QString category() const
Definition: klflib.h:86
The Category to which eq. belongs (path-style string)
Definition: klflib.h:66
void slotUpdateFromCbx(QComboBox *cbx)
void displayEntry(const KLFLibEntry &entry)
void retranslateUi(bool alsoBaseUi=true)
Tags about the equation (string)
Definition: klflib.h:67
QString preamble
QImage preview() const
Definition: klflib.h:84
removeItem(int index)
An entry (single formula) in the library.
Definition: klflib.h:55
addItems(const QStringList &texts)
isEmpty()
QSize labelOutputFixedSize
Definition: klfconfig.h:183
void displayEntries(const QList< KLFLibEntry > &entries)
unsigned long bg_color
QString tags() const
Definition: klflib.h:87
unsigned long fg_color
int glowEffectRadius
Definition: klfconfig.h:199
KLFStyle style() const
Definition: klflib.h:88

Generated by doxygen 1.8.5