[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klflibbrowser_p.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klflibbrowser_p.h
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: klflibbrowser_p.h 603 2011-02-26 23:14:55Z phfaist $ */
23 
29 #ifndef KLFLIBBROWSER_P_H
30 #define KLFLIBBROWSER_P_H
31 
32 #include <QWidget>
33 #include <QStackedWidget>
34 #include <QTabBar>
35 #include <QTabWidget>
36 #include <QEvent>
37 #include <QDragMoveEvent>
38 #include <QDialog>
39 #include <QStackedLayout>
40 #include <QDir>
41 #include <QFileDialog>
42 #include <QFileInfo>
43 #include <QAction>
44 #include <QSettings>
45 #include <QSignalMapper>
46 #include <QDesktopServices>
47 #include <QMessageBox>
48 
49 #include <ui_klflibexportdialog.h>
50 
51 #include <klfsearchbar.h>
52 #include "klflibview.h"
53 #include "klfconfig.h"
54 #include "klflibbrowser.h"
55 
58 {
59  Q_OBJECT
60 public:
62  : QStackedWidget(parent), pResource(resource)
63  {
65 
66  KLF_DEBUG_WATCH_OBJECT(this) ;
67 
68  pResFlags = 0x0;
69 
71  QSignalMapper *actionSignalMapper = new QSignalMapper(this);
72 
73  if (pResource == NULL) {
74  qWarning()<<"KLFLibBrowserViewContainer: NULL RESOURCE! Expect Imminent Crash!";
75  }
76  // find OK view type identifiers
78  int k;
79  for (k = 0; k < allViewTypeIdents.size(); ++k) {
80  QString vtype = allViewTypeIdents[k];
81  KLFLibViewFactory *factory =
83  bool thisViewTypeIdentOk = factory->canCreateLibView(vtype, pResource);
84  if (thisViewTypeIdentOk)
85  pOkViewTypeIdents << vtype;
86  // create an action for this view type
87  QAction *action = new QAction(pViewTypeActionGroup);
88  action->setText(factory->viewTypeTitle(vtype));
89  action->setCheckable(true);
90  connect(action, SIGNAL(triggered(bool)), actionSignalMapper, SLOT(map()));
91  actionSignalMapper->setMapping(action, vtype);
92  action->setEnabled(thisViewTypeIdentOk);
93  action->setData(vtype);
94  pViewTypeActions << action;
95  }
96  connect(actionSignalMapper, SIGNAL(mapped(const QString&)), this, SLOT(openView(const QString&)));
97 
98  connect(this, SIGNAL(currentChanged(int)), this, SLOT(slotCurrentChanged(int)));
99  }
101  {
103  }
104 
105  QUrl url() const {
107 
109  if (view == NULL)
110  return QUrl();
111  return view->url();
112  }
113 
115 
117  KLFAbstractLibView * v = qobject_cast<KLFAbstractLibView*>(currentWidget());
118  if (v == NULL) { klfDbg("view is NULL ..."); }
119  return v;
120  }
121 
123  return pOpenViewTypeIdents.key(currentIndex());
124  }
125 
127 
129 
131 
132  void setResourceRoleFlags(uint flags) {
133  pResFlags = flags;
134  }
135  uint resourceRoleFlags() const { return pResFlags; }
136 
137  QVariantMap saveGuiState() const {
139 
140  QVariantMap v;
141  QVariantList vlist; // will hold QVariantMap's of each view's state
142  QString curViewTypeIdent;
143  QStringList saved_view_types;
145  cit != pOpenViewTypeIdents.end(); ++cit) {
146  KLFAbstractLibView *view = qobject_cast<KLFAbstractLibView*>(widget(*cit));
147  QString vti = cit.key(); // view type identifier
148  if (view == currentWidget()) // this is current view -> save it in more global setting
149  curViewTypeIdent = vti;
150  QVariantMap vstate;
151  vstate["ViewTypeIdentifier"] = vti;
152  vstate["ViewState"] = QVariant::fromValue<QVariantMap>(view->saveGuiState());
153  vlist << QVariant::fromValue<QVariantMap>(vstate);
154  saved_view_types << vti;
155  }
156  // add also the states that are in pViewState from a previous loadGuiState() and for
157  // which no view was opened.
158  QVariantList lastViewStateList = pViewState.value("StateList").toList();
159  for (QVariantList::const_iterator cit = lastViewStateList.begin();
160  cit != lastViewStateList.end(); ++cit) {
161  const QVariantMap& vmap = (*cit).toMap();
162  // see if we have this view type already
163  if (saved_view_types.contains(vmap.value("ViewTypeIdentifier").toString()))
164  continue;
165  // otherwise, add this view state map too
166  vlist << QVariant::fromValue<QVariantMap>(vmap);
167  }
168 
169 
170  v["StateList"] = QVariant::fromValue<QVariantList>(vlist);
171  // Remember: this next key is queried also by KLFLibBrowser::openResourceByGuiState()
172  v["CurrentViewTypeIdentifier"] = QVariant::fromValue<QString>(curViewTypeIdent);
173  return v;
174  }
175  void loadGuiState(const QVariantMap& v)
176  {
178 
179  pViewState = v;
180  klfDbg("loading view state map: "<<v) ;
181 
182  const QVariantList vlist = v["StateList"].toList(); // will hold QVariantMap's
183  const QString curvti = v["CurrentViewTypeIdentifier"].toString();
184  int k;
185  for (k = 0; k < vlist.size(); ++k) {
186  const QVariantMap vstate = vlist[k].toMap();
187  QString vti = vstate["ViewTypeIdentifier"].toString();
188  klfDbg("considering to restore view type "<<vti) ;
189  if (vti != curvti)
190  continue;
191  klfDbg("restore view type "<<vti) ;
192 
193  // open the current view type identifier. The other view type identifiers will have
194  // their state restored only if the user requests to display that view type.
195  bool res = openView(vti);
196  if (!res) {
197  qWarning()<<"Can't open view of type "<<vti<<" for resource "<<url()<<"!";
198  continue;
199  }
201  = qobject_cast<KLFAbstractLibView*>(widget(pOpenViewTypeIdents.value(vti)));
202  if (view == NULL)
203  continue;
204  view->restoreGuiState(vstate["ViewState"].toMap());
205  }
206 
207  // raise current view ...
208  if (!curvti.isEmpty())
209  openView(curvti);
210  }
211 
213  {
215 
217  return QList<QAction*>();
218 
219  QList<QAction*> actions;
220 
221  QStringList subreslist = pResource->subResourceList();
222  QUrl baseurl = pResource->url();
223  int k;
224  for (k = 0; k < subreslist.size(); ++k) {
225  QString t;
227  t = pResource->subResourceProperty(subreslist[k],
229  else
230  t = subreslist[k];
231 
232  QUrl url = baseurl;
233  url.addQueryItem("klfDefaultSubResource", subreslist[k]);
234  QString urlstr = url.toString();
235 
236  QAction *a = NULL;
237  if (pOpenSubResActionCache.contains(urlstr)) {
238  klfDbg("re-using action for url="<<urlstr<<" from our local action cache") ;
239  a = pOpenSubResActionCache[urlstr];
240  } else {
241  klfDbg("creating action for url="<<urlstr<<", it is not yet in our local action cache...") ;
242  a = new QAction(t, this);
243  a->setData(url.toString());
244  connect(a, SIGNAL(triggered()), this, SLOT(internalRequestOpenSubResourceSender()));
245  pOpenSubResActionCache[urlstr] = a;
246  KLF_DEBUG_WATCH_OBJECT(a) ;
247  }
248  actions << a;
249  klfDbg(pResource->url().toString()<<": Added action for sub-resource "<<subreslist[k]<<": url is "
250  <<a->data().toString()) ;
251  }
252 
253  return actions;
254  }
255 
256 public slots:
257  bool openView(const QString& viewTypeIdent)
258  {
260  klfDbg("view type="<<viewTypeIdent<<"; resource URL="<<pResource->url()) ;
261 
262  // see if we already have this view type open and ready
263  if (pOpenViewTypeIdents.contains(viewTypeIdent)) {
264  klfDbg( ": view type "<<viewTypeIdent<<" is already open at index ="
265  <<pOpenViewTypeIdents[viewTypeIdent] ) ;
266  setCurrentIndex(pOpenViewTypeIdents[viewTypeIdent]);
267  return true;
268  }
269  // otherwise, instanciate view
270  KLFLibViewFactory *factory =
271  KLFLibViewFactory::findFactoryFor(viewTypeIdent);
272  KLFAbstractLibView *v = factory->createLibView(viewTypeIdent, this, pResource);
273  if (v == NULL) {
274  qWarning() << "The factory can't open a view of type "<<viewTypeIdent<<"!";
275  return false;
276  }
277  // some view settings as given by klfconfig
278  if (viewTypeIdent == "default") { // category tree list
279  KLFLibDefaultView *defview = qobject_cast<KLFLibDefaultView*>(v);
280  if (defview == NULL) {
281  qWarning()<<KLF_FUNC_NAME<<": Created view of type"<<viewTypeIdent<<", that is NOT a KLFLibDefaultView ?!?";
282  } else {
284  }
285  }
286 
287  KLF_DEBUG_WATCH_OBJECT(v) ;
288 
289  v->setContextMenuPolicy(Qt::CustomContextMenu);
290  // get informed about selection changes
291  connect(v, SIGNAL(entriesSelected(const KLFLibEntryList& )),
292  this, SLOT(slotEntriesSelected(const KLFLibEntryList& )));
293  // and of new category suggestions
294  connect(v, SIGNAL(moreCategorySuggestions(const QStringList&)),
295  this, SLOT(slotMoreCategorySuggestions(const QStringList&)));
296  // and of restore actions
297  connect(v, SIGNAL(requestRestore(const KLFLibEntry&, uint)),
298  this, SLOT(slotRequestRestore(const KLFLibEntry&, uint)));
299  connect(v, SIGNAL(requestRestoreStyle(const KLFStyle&)),
300  this, SLOT(slotRequestRestoreStyle(const KLFStyle&)));
301  // and of after-change refresh
302  connect(v, SIGNAL(resourceDataChanged(const QList<KLFLib::entryId>&)),
303  this, SLOT(slotResourceDataChanged(const QList<KLFLib::entryId>&)));
304  // and of context menu request
305  connect(v, SIGNAL(customContextMenuRequested(const QPoint&)),
306  this, SIGNAL(viewContextMenuRequested(const QPoint&)));
307  // view progress reporting
308  connect(v, SIGNAL(operationStartReportingProgress(KLFProgressReporter *, const QString&)),
310 
311  klfDbgT(": connected signals.");
316  else
317  pResource->setViewType(viewTypeIdent);
318  klfDbgT(": set view type.");
319 
320  int curindex = currentIndex();
321  blockSignals(true);
322  int index = addWidget(v);
323  blockSignals(false);
324  pOpenViewTypeIdents[viewTypeIdent] = index;
325  klfDbgT(": added widget, about to raise");
326  if (currentIndex() == curindex)
327  setCurrentIndex(index); // show new open view (and call slot slotCurrentIndex())
328  else {
329  // call now the slot that would have been called with addWidget()
330  // if the signals hadn't been blocked
331  slotCurrentChanged(index);
332  }
333  klfDbgT(": Added view.");
334 
335  // see if we have a GUI state to restore
336  if (pViewState.contains("StateList")) {
337  const QVariantList& vlist = pViewState["StateList"].toList(); // will hold QVariantMap's
338  klfDbg("possibly restoring a GUI state... list size="<<vlist.size()) ;
339  int k;
340  for (k = 0; k < vlist.size(); ++k) {
341  const QVariantMap& vstate = vlist[k].toMap();
342  QString vti = vstate["ViewTypeIdentifier"].toString();
343  if (vti != viewTypeIdent)
344  continue;
345  klfDbg("Restoring gui state, match found for vti[="<<vti<<"]==viewTypeIdent[="<<viewTypeIdent<<"]!") ;
346  v->restoreGuiState(vstate["ViewState"].toMap());
347  break;
348  }
349  }
350  return true;
351  }
352 
353 
354 signals:
355  void viewContextMenuRequested(const QPoint& pos);
356 
357  void viewTypeChanged(const QString&);
358 
359  void requestRestore(const KLFLibEntry& entry, uint restoreflags = KLFLib::RestoreLatexAndStyle);
360  void requestRestoreStyle(const KLFStyle& style);
361 
362  void resourceDataChanged(const QList<KLFLib::entryId>& entryIdList);
363 
364  void entriesSelected(const KLFLibEntryList& entries);
365  void moreCategorySuggestions(const QStringList& categorylist);
366 
367  void requestOpenUrl(const QString& url);
368 
369  void viewOperationStartReportingProgress(KLFProgressReporter *progressReporter, const QString& descriptiveText);
370 
371 protected slots:
372  void slotRequestRestore(const KLFLibEntry& entry, uint restoreflags = KLFLib::RestoreLatexAndStyle) {
373  if (sender() == view())
374  emit requestRestore(entry, restoreflags);
375  }
376  void slotRequestRestoreStyle(const KLFStyle& style) {
377  if (sender() == view())
378  emit requestRestoreStyle(style);
379  }
381  if (sender() == view())
382  emit resourceDataChanged(entryIdList);
383  }
384  void slotEntriesSelected(const KLFLibEntryList& entries) {
385  if (sender() == view())
386  emit entriesSelected(entries);
387  }
388  void slotMoreCategorySuggestions(const QStringList& categorylist) {
389  if (sender() == view())
390  emit moreCategorySuggestions(categorylist);
391  }
392 
393  void slotCurrentChanged(int index) {
395 
396  KLFAbstractLibView *v = view();
397  if (v == NULL) {
398  setSearchTarget(NULL);
399  return;
400  }
402  QString vtype = pOpenViewTypeIdents.key(index);
403  int ai = findViewTypeAction(vtype);
404  if (ai >= 0)
405  pViewTypeActions[ai]->setChecked(true);
406 
407 
408  emit viewTypeChanged(vtype);
409  emit entriesSelected(v->selectedEntries());
410  }
411 
413  {
415 
416  QAction * a = qobject_cast<QAction*>(sender());
417  KLF_ASSERT_NOT_NULL( a, "Sender action is NULL", return ) ;
418 
419  emit requestOpenUrl(a->data().toString());
420  }
421 
422 protected:
425 
426  QVariantMap pViewState;
427 
428 
433 
434  uint pResFlags;
435 
436  int findViewTypeAction(const QString& vtype) {
438 
439  int k;
440  for (k = 0; k < pViewTypeActions.size(); ++k)
441  if (pViewTypeActions[k]->data().toString() == vtype)
442  return k;
443  return -1;
444  }
445 
446  // re-use "open sub-resource ->" actions within multiple menu appearances
448 };
449 
450 
451 // ------------------------
452 
453 
456 {
457  Q_OBJECT
458 public:
460  {
461  setUsesScrollButtons(false);
462  pTabBar = new QTabBar(this);
463  pTabBar->setAcceptDrops(true);
464  pTabBar->installEventFilter(this);
465  setTabBar(pTabBar);
466  }
468 
470  int getTabAtPoint(const QPoint& pos) {
471  QPoint tabbarpos = pTabBar->mapFromGlobal(mapToGlobal(pos));
472  return pTabBar->tabAt(tabbarpos);
473  }
474 
476  QRect getTabRect(int tab) {
477  QRect tabbarrect = pTabBar->tabRect(tab);
478  return QRect(mapFromGlobal(pTabBar->mapToGlobal(tabbarrect.topLeft())),
479  tabbarrect.size());
480  }
481 
482  bool eventFilter(QObject *object, QEvent *event)
483  {
484  if (object == pTabBar && event->type() == QEvent::DragEnter) {
485  QDragEnterEvent *de = (QDragEnterEvent*)event;
486  de->accept();
487  return true;
488  }
489  if (object == pTabBar && event->type() == QEvent::DragMove) {
490  // ignore event, but raise the underlying tab.
491  QDragMoveEvent *de = (QDragMoveEvent*)event;
492  QPoint pos = de->pos();
493  de->ignore();
494  int index = getTabAtPoint(pos);
495  if (index >= 0)
496  setCurrentIndex(index);
497  return true;
498  }
499  return QTabWidget::eventFilter(object, event);
500  }
501 
502 
503  void setTabEnabled(int index, bool enable)
504  {
505  QTabWidget::setTabEnabled(index, enable);
506  emit pageEnabled(index, enable);
507  }
508 
509  void setTabIcon(int index, const QIcon& icon)
510  {
511  QTabWidget::setTabIcon(index, icon);
512  emit pageIconChanged(index, icon);
513  }
514 
515  void setTabText(int index, const QString& label)
516  {
517  QTabWidget::setTabText(index, label);
518  emit pageTextChanged(index, label);
519  }
520 
521 signals:
522 
523  void pageInserted(int index, const QIcon& icon, const QString& text);
524  void pageTextChanged(int index, const QString& text);
525  void pageIconChanged(int index, const QIcon& icon);
526  void pageEnabled(int index, bool enable);
527  void pageRemoved(int index);
528 
529 
530 public slots:
531 
532  void refreshTabReadOnly(int tabindex, bool readonly) {
533  QPalette pal = palette();
534 
535  QColor normalColor = pal.color(QPalette::Active, QPalette::ButtonText);
536  QColor readOnlyColor = pal.color(QPalette::Disabled, QPalette::ButtonText);
537 
538  if (readonly)
539  pTabBar->setTabTextColor(tabindex, readOnlyColor);
540  else
541  pTabBar->setTabTextColor(tabindex, normalColor);
542  }
543 
544 protected:
545 
546  virtual void tabInserted(int index) {
547  emit pageInserted(index, tabIcon(index), tabText(index));
548  }
549  virtual void tabRemoved(int index) {
550  emit pageRemoved(index);
551  }
552 
553 private:
554  QTabBar *pTabBar;
555 };
556 
557 // -----------------------------------------------------------------
558 
559 
560 
562 {
563  Q_OBJECT
564 public:
566  : QMenu(tabwidget), pTabWidget(tabwidget)
567  {
568  setTitle(tr("Switch to Tab"));
569  pActionGroup = new QActionGroup(this);
570  pActionGroup->setExclusive(true);
571 
572  int k;
573  for (k = 0; k < pTabWidget->count(); ++k) {
574  slotPageInserted(k, pTabWidget->tabIcon(k), pTabWidget->tabText(k));
575  }
576 
577  connect(pTabWidget, SIGNAL(pageInserted(int, const QIcon&, const QString&)),
578  this, SLOT(slotPageInserted(int, const QIcon&, const QString&)));
579  connect(pTabWidget, SIGNAL(pageRemoved(int)), this, SLOT(slotPageRemoved(int)));
580  connect(pTabWidget, SIGNAL(pageTextChanged(int, const QString&)),
581  this, SLOT(slotPageTextChanged(int, const QString&)));
582  connect(pTabWidget, SIGNAL(pageIconChanged(int, const QIcon&)),
583  this, SLOT(slotPageIconChanged(int, const QIcon&)));
584  connect(pTabWidget, SIGNAL(pageEnabled(int, bool)), this, SLOT(slotPageEnabled(int, bool)));
585  connect(pTabWidget, SIGNAL(currentChanged(int)), this, SLOT(slotCurrentIndexChanged(int)));
586  }
588  {
589  }
590 
591 protected slots:
592  void slotPageInserted(int index, const QIcon& icon, const QString& text)
593  {
595  QAction *a = new QAction(pActionGroup);
596  a->setIcon(icon);
597  a->setText(text);
598  a->setCheckable(true);
599  connect(a, SIGNAL(triggered()), this, SLOT(slotRaisePageFromAction()));
600  if (index < actions().size())
601  insertAction(actions()[index], a);
602  else
603  addAction(a);
604  }
605  void slotPageRemoved(int index)
606  {
608  if (index >= 0 && index < actions().size())
609  removeAction(actions()[index]);
610  }
611  void slotPageTextChanged(int index, const QString& text)
612  {
613  if (index >= 0 && index < actions().size())
614  actions()[index]->setText(text);
615  }
616  void slotPageIconChanged(int index, const QIcon& icon)
617  {
618  if (index >= 0 && index < actions().size())
619  actions()[index]->setIcon(icon);
620  }
621  void slotPageEnabled(int index, bool enable)
622  {
623  if (index >= 0 && index < actions().size())
624  actions()[index]->setEnabled(enable);
625  }
626  void slotCurrentIndexChanged(int index)
627  {
628  if (index >= 0 && index < actions().size())
629  actions()[index]->setChecked(true); // will auto-exclude the others because of exclusive action group
630  }
631 
632 
634  {
635  QAction * a = qobject_cast<QAction*>(sender());
636  if (a == NULL) {
637  qWarning()<<KLF_FUNC_NAME<<": action sender is NULL?!?";
638  return;
639  }
640  int k;
641  QList<QAction*> alist = actions();
642  for (k = 0; k < alist.size(); ++k) {
643  if (alist[k] == a) {
644  pTabWidget->setCurrentIndex(k);
645  return;
646  }
647  }
648  }
649 
650 private:
651  QActionGroup *pActionGroup;
652  KLFLibBrowserTabWidget *pTabWidget;
653 };
654 
655 
656 
657 
658 
659 
660 // -----------------------------------------------------------------
661 
662 
664 class KLFLibExportDialog : public QDialog, public Ui::KLFLibExportDialog
665 {
666  Q_OBJECT
667 public:
668  enum { DataRole = Qt::UserRole, OldCheckStateRole } ;
669 
671  {
672  KLFLibExportDialog dlg(libbrowser);
673  bool ok;
675  *exportUrls = QList<QUrl>();
676  do {
677  int result = dlg.exec();
678  if (result != QDialog::Accepted)
679  return NULL; // cancelled by user
680  param = dlg.wfactory->retrieveCreateParametersFromWidget("LocalFile", dlg.localFileWidget);
681  ok = true; // by default we got parameters OK
682  if (param.isEmpty())
683  ok = false;
684  if (param.contains("klfRetry") && param["klfRetry"].toBool())
685  ok = false;
686  if (param.contains("klfCancel") && param["klfCancel"].toBool())
687  return NULL; // cancelled
688  } while (!ok) ;
689 
690  QList<QUrl> urls = dlg.selectedExportUrls();
691  if (urls.isEmpty()) {
692  QMessageBox::critical(&dlg, tr("Error"), tr("You have not selected any resources to export!"));
693  return NULL;
694  }
695 
696  // now create the resource
697  if (!param.contains("klfScheme")) {
698  qWarning()<<KLF_FUNC_NAME<<": factory create parameters did not provide a scheme!";
699  return NULL;
700  }
701  QString scheme = param["klfScheme"].toString();
703  if (efactory == NULL) {
704  qWarning()<<KLF_FUNC_NAME<<": can't find factory for scheme "<<scheme<<"!";
705  return NULL;
706  }
707  // this is the sub-resource that will be created with the resource. It will be deleted after
708  // we filled up the resource. Remember the name ("export_xtra"), it will be deleted at the
709  // end of KLFLibBrowser::slotExport(), ie. don't change this without consideration.
710  param["klfDefaultSubResource"] = QLatin1String("export_xtra");
711 
712  KLFLibResourceEngine *resource = efactory->createResource(scheme, param, libbrowser);
713  if (resource == NULL) {
714  QMessageBox::critical(&dlg, tr("Error"),
715  tr("Can't create resource %1!").arg(param["Filename"].toString()));
716  qWarning()<<KLF_FUNC_NAME<<": Failed to create resource of type "<<scheme<<" with parameters: "<<param;
717  return NULL;
718  }
719  *exportUrls = urls;
720  return resource;
721  }
722 
723 
725  : QDialog(libbrowser, 0), pLibBrowser(libbrowser), pModel(NULL),
726  pInSlotItemChanged(false)
727  {
728  setupUi(this);
729  setWindowModality(Qt::WindowModal);
730  setModal(true);
731 
732  wfactory = KLFLibWidgetFactory::findFactoryFor("LocalFile");
733  if (wfactory == NULL) {
734  qWarning()<<KLF_FUNC_NAME<<": Can't find factory for type 'LocalFile'!";
735  return;
736  }
739  QString filedate = QDate::currentDate().toString("yyyy-MM-dd");
740  pdefault["Url"] = QUrl("klf+legacy://"+tr("%1/klatexformula_export_%2.klf").arg(mydir, filedate));
741  QStackedLayout *lyt_gbxExportLocalFileWidget = new QStackedLayout(gbxExportLocalFileWidget);
742  lyt_gbxExportLocalFileWidget->setObjectName("lyt_gbxExportLocalFileWidget");
743  lyt_gbxExportLocalFileWidget->setContentsMargins(0,0,0,0);
744  lyt_gbxExportLocalFileWidget->setSpacing(0);
745  localFileWidget =
746  wfactory->createPromptCreateParametersWidget(gbxExportLocalFileWidget, "LocalFile", pdefault);
747  lyt_gbxExportLocalFileWidget->addWidget(localFileWidget);
748  localFileWidget->setFixedHeight(localFileWidget->minimumSizeHint().height()+4);
749  gbxExportLocalFileWidget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
750 
751  pModel = new QStandardItemModel(this);
752  lstRes->setModel(pModel);
753 
754  connect(pModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
755 
756  connect(btnSelectAll, SIGNAL(clicked()), this, SLOT(selectAll()));
757  connect(btnUnselectAll, SIGNAL(clicked()), this, SLOT(unselectAll()));
758 
759  populateExportList();
760  }
761 
762 
764  {
765  QList<QUrl> urlList;
766  int k, j;
767  QStandardItem *root = pModel->invisibleRootItem();
768  for (k = 0; k < root->rowCount(); ++k) {
769  QStandardItem *resItem = root->child(k);
770  if (resItem->rowCount() > 0) {
771  // has specific sub-resources
772  for (j = 0; j < resItem->rowCount(); ++j) {
773  if (resItem->child(j)->checkState() != Qt::Unchecked) {
774  QUrl url = resItem->data(DataRole).toString();
775  url.addQueryItem("klfDefaultSubResource", resItem->child(j)->data(DataRole).toString());
776  urlList << url;
777  continue;
778  }
779  }
780  } else {
781  // no specific sub-resources, ie. the resource does not support sub-resources
782  if (resItem->checkState() != Qt::Unchecked)
783  urlList << QUrl(resItem->data(DataRole).toString());
784  }
785  }
786  return urlList;
787  }
788 
789 
790 public slots:
791 
792  void selectAll(bool select = true)
793  {
794  // select all root items, their children will follow by cascade
795  QStandardItem *root = pModel->invisibleRootItem();
796  int k;
797  for (k = 0; k < root->rowCount(); ++k) {
798  root->child(k)->setCheckState(select ? Qt::Checked : Qt::Unchecked);
799  }
800  }
801  void unselectAll()
802  {
803  selectAll(false);
804  }
805 
806 private slots:
807 
808  void slotItemChanged(QStandardItem *item)
809  {
810  if (pInSlotItemChanged)
811  return;
812  pInSlotItemChanged = true;
813  Qt::CheckState chk = item->checkState();
814  Qt::CheckState oldchk = (Qt::CheckState)item->data(OldCheckStateRole).toInt();
815  // printf("Item %p maybe check state changed from %d to %d?\n", (void*)item,
816  // oldchk, chk);
817  if ((chk == Qt::Checked || chk == Qt::Unchecked) && oldchk != chk) {
818  // printf("Item %p check state changed from %d to %d!\n", (void*)item, oldchk, chk);
819  // item was checked or un-checked
820  item->setData(chk, OldCheckStateRole);
821  // iterate its children and (un-)check them all
822  int k;
823  for (k = 0; k < item->rowCount(); ++k) {
824  // printf("-->\n");
825  item->child(k)->setData(chk, OldCheckStateRole); // no update please
826  item->child(k)->setCheckState(chk);
827  // printf("<--\n");
828  }
829  // and now set the parent's item check state to partially checked.
830  QStandardItem *p = item->parent();
831  if (p != NULL) {
832  // printf("parent-->\n");
833  p->setData(Qt::PartiallyChecked, OldCheckStateRole);
834  p->setCheckState(Qt::PartiallyChecked);
835  // printf("<--parent\n");
836  }
837  // printf("Done processing item %p check state change from %d to %d\n", (void*)item, oldchk, chk);
838  }
839  pInSlotItemChanged = false;
840  }
841 
842 
843 private:
844  KLFLibBrowser *pLibBrowser;
845  QStandardItemModel *pModel;
846  bool pInSlotItemChanged;
847 
848  KLFLibWidgetFactory * wfactory;
849  QWidget *localFileWidget;
850 
851 
852  void populateExportList()
853  {
854  pModel->clear();
855  pModel->setColumnCount(1);
856  pModel->setHorizontalHeaderLabels(QStringList()<<tr("Resource", "[[export list title]]"));
857 
858  QList<QUrl> openurls = pLibBrowser->openUrls();
859  int k, j;
860  for (k = 0; k < openurls.size(); ++k) {
861  KLFLibResourceEngine *res = pLibBrowser->getOpenResource(openurls[k]);
862  QStandardItem *parent = getResourceParentItem(res);
863  QStringList subreslist;
864  if (openurls[k].hasQueryItem("klfDefaultSubResource")) {
865  subreslist = QStringList() << openurls[k].queryItemValue("klfDefaultSubResource");
867  // no default sub-resource is specified, so add them all
868  subreslist = res->subResourceList();
869  }
870  // add all subresources
871  for (j = 0; j < subreslist.size(); ++j) {
872  if (findSubResourceItem(res, subreslist[j]) != NULL)
873  continue;
874  // create this item
875  QStandardItem *item = new QStandardItem;
876  item->setData(subreslist[j], DataRole);
877  QString srtitle;
879  srtitle =
881  if (srtitle.isEmpty())
882  srtitle = subreslist[j];
883  item->setText(srtitle);
884  item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
885  item->setCheckState(Qt::Checked);
886  item->setData(item->checkState(), OldCheckStateRole);
887  parent->appendRow(item);
888  lstRes->setExpanded(pModel->indexFromItem(parent), true);
889  }
890  }
891  }
892 
893  QStandardItem *getResourceParentItem(KLFLibResourceEngine *res)
894  {
895  QStandardItem *root = pModel->invisibleRootItem();
896  int j;
897  for (j = 0; j < root->rowCount(); ++j) {
898  if (root->child(j)->data(DataRole).value<QString>() == res->url().toString())
899  return root->child(j);
900  }
901  // not existent, need to create it
902  QStandardItem *item = new QStandardItem;
903  item->setData(QVariant::fromValue<QString>(res->url().toString()), DataRole);
904  item->setText(res->title());
905  item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
906  item->setCheckState(Qt::Checked);
907  item->setData(item->checkState(), OldCheckStateRole);
908  root->appendRow(item);
909  return item;
910  }
911 
912  QStandardItem *findSubResourceItem(KLFLibResourceEngine *res, const QString& subResource)
913  {
914  QStandardItem *parent = getResourceParentItem(res);
915  int k;
916  for (k = 0; k < parent->rowCount(); ++k) {
917  if (parent->child(k)->data(DataRole) == subResource)
918  return parent->child(k);
919  }
920  return NULL;
921  }
922 };
923 
924 
925 
926 
927 
928 /* THIS HAS NEVER BEEN USED NOR TESTED, HOWEVER IT MAY COME IN HANDY IF NEEDED.
929 
930 / ** \internal * /
931 class KLFSignalUnmapper : public QObject
932 {
933  Q_OBJECT
934 public:
935  KLFSignalUnmapper(QObject *parent) : QObject(parent)
936  {
937  }
938  virtual ~KLFSignalUnmapper() { }
939 
940  void registerObject(const QString& key, QObject *object, const QByteArray& member,
941  QList<QGenericArgument> staticArgs)
942  {
943  UnmappedObject o;
944  o.key = key;
945  o.object = object;
946  o.member = member;
947  o.staticArgs = staticArgs;
948  unmappedObjects << o;
949  connect(o.object, SIGNAL(destroyed()), this, SLOT(unregisterObjectSender()));
950  }
951 
952 public slots:
953  void unmap(const QString& key) {
954  int k;
955  for (k = 0; k < unmappedObjects.size(); ++k) {
956  if (unmappedObjects[k].key == key) {
957  const QList<QGenericArgument>& al = unmappedObjects[k].staticArgs;
958  QMetaObject::invokeMethod(unmappedObjects[k].object, unmappedObjects[k].member,
959  al.value(0), al.value(1), al.value(2), al.value(3), al.value(4),
960  al.value(5), al.value(6), al.value(7), al.value(8), al.value(9));
961  }
962  }
963  }
964 
965  void unregisterObject(QObject *object) {
966  int k;
967  for (k = 0; k < unmappedObjects.size(); ++k)
968  if (unmappedObjects[k].object == object)
969  unmappedObjects.removeAt(k);
970  }
971 
972 private slots:
973  void unregisterObjectSender() {
974  QObject *object = sender();
975  if (object == NULL)
976  return;
977  unregisterObject(object);
978  }
979 
980 private:
981  struct UnmappedObject {
982  QString key;
983  QObject *object;
984  QByteArray member;
985  QList<QGenericArgument> staticArgs;
986  };
987  QList<UnmappedObject> unmappedObjects;
988 };
989 */
990 
991 
992 
993 #endif
void setSearchTarget(KLFSearchable *target)
void slotMoreCategorySuggestions(const QStringList &categorylist)
void setGroupSubCategories(bool yesOrNo)
Definition: klflibview.h:836
void slotPageEnabled(int index, bool enable)
QList< QUrl > openUrls() const
KLFLibResourceEngine * resourceEngine()
QVariantMap saveGuiState() const
QList< QUrl > selectedExportUrls()
virtual QString viewTypeTitle(const QString &viewTypeIdent) const =0
ignore(const QRect &rectangle)
bool eventFilter(QObject *object, QEvent *event)
void requestOpenUrl(const QString &url)
void slotPageIconChanged(int index, const QIcon &icon)
contains(const Key &key)
static QStringList allSupportedViewTypeIdentifiers()
Definition: klflibview.cpp:249
bool groupSubCategories
Definition: klfconfig.h:241
virtual QVariant subResourceProperty(const QString &subResource, int propId) const
Definition: klflib.cpp:528
void loadGuiState(const QVariantMap &v)
virtual KLFAbstractLibView * createLibView(const QString &viewTypeIdent, QWidget *parent, KLFLibResourceEngine *resourceEngine)=0
virtual bool canCreateLibView(const QString &viewTypeIdent, KLFLibResourceEngine *engine)=0
void setTabEnabled(int index, bool enable)
virtual QWidget * createPromptCreateParametersWidget(QWidget *wparent, const QString &wtype, const Parameters &defaultparameters=Parameters())
Definition: klflibview.cpp:311
void slotPageTextChanged(int index, const QString &text)
tabText(int index)
QList< QAction * > pViewTypeActions
void slotCurrentChanged(int index)
#define klfDbgT(streamableItems)
Sub-Resources may be assigned properties and values.
Definition: klflib.h:515
KLFConfig klfconfig
Definition: klfconfig.cpp:88
KLFLibResourceEngine * getOpenResource(const QUrl &url)
tabRect(int index)
static KLFLibWidgetFactory * findFactoryFor(const QString &wtype)
Definition: klflibview.cpp:294
color(ColorGroup group, ColorRole role)
virtual QVariantMap saveGuiState() const =0
contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive)
virtual bool setSubResourceProperty(const QString &subResource, int propId, const QVariant &value)
Definition: klflib.cpp:593
#define klfDbg(streamableItems)
KLFLibResourceEngine * pResource
triggered(QAction *action)
void viewContextMenuRequested(const QPoint &pos)
QList< QAction * > openSubResourceActions()
#define KLF_DEBUG_BLOCK(msg)
setHorizontalHeaderLabels(const QStringList &labels)
virtual bool setViewType(const QString &viewType)
Definition: klflib.cpp:569
void viewOperationStartReportingProgress(KLFProgressReporter *progressReporter, const QString &descriptiveText)
KLFLibBrowserTabWidget(QWidget *parent)
bool openView(const QString &viewTypeIdent)
setTabEnabled(int index, bool enable)
toString(FormattingOptions options=None)
virtual void tabRemoved(int index)
addAction(const QString &text)
KLFAbstractLibView * view()
void selectAll(bool select=true)
void pageInserted(int index, const QIcon &icon, const QString &text)
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
setData(const QVariant &value, int role=Qt::UserRole+1)
static KLFLibViewFactory * findFactoryFor(const QString &viewTypeIdentifier)
Definition: klflibview.cpp:232
void slotPageInserted(int index, const QIcon &icon, const QString &text)
setTabText(int index, const QString &label)
void refreshTabReadOnly(int tabindex, bool readonly)
setMapping(QObject *sender, int id)
virtual bool restoreGuiState(const QVariantMap &state)=0
setTabTextColor(int index, const QColor &color)
setColumnCount(int columns)
void pageTextChanged(int index, const QString &text)
indexFromItem(const QStandardItem *item)
QStringList supportedViewTypeIdentifiers() const
appendRow(const QList< QStandardItem * > &items)
setFlags(Qt::ItemFlags flags)
Data can be stored in separate sub-resources.
Definition: klflib.h:508
label(int index)
int findViewTypeAction(const QString &vtype)
tabIcon(int index)
void slotRequestRestoreStyle(const KLFStyle &style)
A view widget to display a library resource's contents.
Definition: klflibview.h:82
virtual KLFLibResourceEngine * createResource(const QString &scheme, const Parameters &parameters, QObject *parent=NULL)
Create a new resource of given type and parameters.
Definition: klflib.cpp:1178
QMap< QString, QAction * > pOpenSubResActionCache
void setTabText(int index, const QString &label)
void slotResourceDataChanged(const QList< KLFLib::entryId > &entryIdList)
virtual QString defaultSubResource() const
Definition: klflib.cpp:504
void setTabIcon(int index, const QIcon &icon)
#define KLF_DEBUG_TIME_BLOCK(msg)
child(int row, int column=0)
KLFLibBrowserViewContainer(KLFLibResourceEngine *resource, QTabWidget *parent)
setData(const QVariant &userData)
virtual QUrl url() const =0
Display Resource URL. NOT exactly like KLFLibResourceEngine::url() !
virtual uint supportedFeatureFlags() const
List of features supported by this resource engine.
Definition: klflib.h:550
#define KLF_FUNC_NAME
virtual KLFLibEntryList selectedEntries() const =0
text(int id)
virtual QStringList subResourceList() const
Definition: klflib.h:722
KLFLibBrowserTabMenu(KLFLibBrowserTabWidget *tabwidget)
virtual QString title() const
The human-set title of this resource.
Definition: klflib.h:599
void resourceDataChanged(const QList< KLFLib::entryId > &entryIdList)
virtual KLFSearchable * searchable()
Definition: klflibview.h:167
void viewTypeChanged(const QString &)
KLFLibExportDialog(KLFLibBrowser *libbrowser)
key(const T &value)
setText(const QString &text)
void pageEnabled(int index, bool enable)
void pageRemoved(int index)
Create Associated Widgets to resources for Open/Create/Save actions.
Definition: klflibview.h:339
QString lastFileDialogPath
Definition: klfconfig.h:244
void setResourceRoleFlags(uint flags)
void slotCurrentIndexChanged(int index)
int getTabAtPoint(const QPoint &pos)
An entry (single formula) in the library.
Definition: klflib.h:55
setFixedHeight(int h)
void requestRestore(const KLFLibEntry &entry, uint restoreflags=KLFLib::RestoreLatexAndStyle)
static KLFLibEngineFactory * findFactoryFor(const QUrl &url)
Definition: klflib.cpp:1163
void requestRestoreStyle(const KLFStyle &style)
virtual Parameters retrieveCreateParametersFromWidget(const QString &wtype, QWidget *widget)
Definition: klflibview.cpp:318
currentDate()
void pageIconChanged(int index, const QIcon &icon)
critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
virtual ~KLFLibBrowserTabMenu()
tabAt(const QPoint &position)
QRect getTabRect(int tab)
addQueryItem(const QString &key, const QString &value)
void slotRequestRestore(const KLFLibEntry &entry, uint restoreflags=KLFLib::RestoreLatexAndStyle)
isEmpty()
static KLFLibResourceEngine * showExportDialogCreateResource(KLFLibBrowser *libbrowser, QList< QUrl > *exportUrls)
setCheckState(Qt::CheckState state)
void entriesSelected(const KLFLibEntryList &entries)
virtual QUrl url(uint flags=0x0) const
query URL
Definition: klflib.cpp:457
An abstract resource engine.
Definition: klflib.h:440
currentChanged(int index)
void moreCategorySuggestions(const QStringList &categorylist)
struct KLFConfig::@4 LibraryBrowser
widget(int index)
addWidget(QWidget *widget)
addWidget(QWidget *widget)
virtual void tabInserted(int index)
void slotPageRemoved(int index)
QActionGroup * pViewTypeActionGroup
data(int role=Qt::UserRole+1)
QMap< QString, int > pOpenViewTypeIdents
QList< QAction * > viewTypeActions() const
value(const Key &key)
void slotEntriesSelected(const KLFLibEntryList &entries)
setTabBar(QTabBar *tb)
setTabIcon(int index, const QIcon &icon)

Generated by doxygen 1.8.6