• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/App

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • App
  • Ui
LoadedPluginsDialog.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "LoadedPluginsDialog.h"
20 #include "GraphFileBackendManager.h"
21 
22 #include <KLocale>
23 #include <KAboutData>
24 #include <KAboutApplicationDialog>
25 #include <KComponentData>
26 #include <KWidgetItemDelegate>
27 #include <KPushButton>
28 #include <KTitleWidget>
29 
30 #include <QApplication>
31 #include <QAbstractListModel>
32 #include <QAbstractItemView>
33 #include <QVBoxLayout>
34 #include <QLabel>
35 #include <QListView>
36 #include <QPainter>
37 #include <QPointer>
38 
39 #define MARGIN 5
40 
41 bool sortPlugins(GraphFilePluginInterface* pluginL, GraphFilePluginInterface* pluginR)
42 {
43  return pluginL->aboutData()->programName() < pluginR->aboutData()->programName();
44 }
45 
46 class PluginsModel : public QAbstractListModel
47 {
48 public:
49  enum ExtraRoles {
50  CommentRole = Qt::UserRole+1
51  };
52  PluginsModel(QObject* parent = 0)
53  : QAbstractListModel(parent)
54  {
55  _plugins = GraphFileBackendManager::self()->backends();
56  qSort(_plugins.begin(), _plugins.end(), sortPlugins);
57  }
58 
59  GraphFilePluginInterface* pluginData(const QModelIndex& index) const
60  {
61  if (!index.isValid()) {
62  return 0;
63  }
64  if (index.parent().isValid()) {
65  return 0;
66  }
67  if (index.column() != 0) {
68  return 0;
69  }
70  if (index.row() >= _plugins.count()) {
71  return 0;
72  }
73  return _plugins[index.row()];
74  }
75 
76  virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
77  {
78  GraphFilePluginInterface* plugin = pluginData(index);
79  if (!plugin) {
80  return QVariant();
81  }
82  switch (role) {
83  case Qt::DisplayRole:
84  {
85  QString name(plugin->aboutData()->programName());
86  return name;
87  }
88  case CommentRole:
89  return plugin->aboutData()->shortDescription();
90  default:
91  return QVariant();
92  };
93  }
94 
95  virtual int rowCount(const QModelIndex& parent = QModelIndex()) const
96  {
97  if (!parent.isValid()) {
98  return _plugins.count();
99  }
100  return 0;
101  }
102 
103 private:
104  QList<GraphFilePluginInterface*> _plugins;
105 };
106 
107 
108 class LoadedPluginsDelegate : public KWidgetItemDelegate
109 {
110  Q_OBJECT
111 
112 public:
113 
114  LoadedPluginsDelegate(QAbstractItemView *itemView, QObject *parent = 0)
115  : KWidgetItemDelegate(itemView, parent)
116  , pushButton(new KPushButton)
117  {
118  pushButton->setIcon(KIcon("dialog-information")); // only for getting size matters
119  }
120 
121  ~LoadedPluginsDelegate()
122  {
123  delete pushButton;
124  }
125 
126  QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
127  {
128  int i = 5;
129  int j = 1;
130 
131  QFont font = titleFont(option.font);
132  QFontMetrics fmTitle(font);
133 
134  return QSize(qMax(fmTitle.width(index.model()->data(index, Qt::DisplayRole).toString()),
135  option.fontMetrics.width(index.model()->data(index, PluginsModel::CommentRole).toString())) +
136  KIconLoader::SizeMedium + MARGIN * i + pushButton->sizeHint().width() * j,
137  qMax(KIconLoader::SizeMedium + MARGIN * 2, fmTitle.height() + option.fontMetrics.height() + MARGIN * 2));
138 
139  }
140 
141  void paint(QPainter *painter, const QStyleOptionViewItem &option,
142  const QModelIndex &index) const
143  {
144  if (!index.isValid()) {
145  return;
146  }
147 
148  painter->save();
149 
150  QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
151 
152  QRect contentsRect(dependantLayoutValue(MARGIN * 2 + option.rect.left(), option.rect.width() - MARGIN * 3, option.rect.width()), MARGIN + option.rect.top(), option.rect.width() - MARGIN * 3, option.rect.height() - MARGIN * 2);
153 
154  int lessHorizontalSpace = MARGIN * 2 + pushButton->sizeHint().width();
155 
156  contentsRect.setWidth(contentsRect.width() - lessHorizontalSpace);
157 
158  if (option.state & QStyle::State_Selected) {
159  painter->setPen(option.palette.highlightedText().color());
160  }
161 
162  if (itemView()->layoutDirection() == Qt::RightToLeft) {
163  contentsRect.translate(lessHorizontalSpace, 0);
164  }
165 
166  painter->save();
167 
168  painter->save();
169  QFont font = titleFont(option.font);
170  QFontMetrics fmTitle(font);
171  painter->setFont(font);
172  painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignTop, fmTitle.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, contentsRect.width()));
173  painter->restore();
174 
175  painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignBottom, option.fontMetrics.elidedText(index.model()->data(index, PluginsModel::CommentRole).toString(), Qt::ElideRight, contentsRect.width()));
176 
177  painter->restore();
178  painter->restore();
179  }
180 
181  QList<QWidget*> createItemWidgets() const
182  {
183  KPushButton *button = new KPushButton();
184  button->setIcon(KIcon("dialog-information"));
185  setBlockedEventTypes(button, QList<QEvent::Type>() << QEvent::MouseButtonPress
186  << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick);
187 
188  connect(button, SIGNAL(clicked(bool)), this, SLOT(info()));
189  return QList<QWidget*>()
190  << button;
191  }
192 
193  void updateItemWidgets(const QList<QWidget*> widgets,
194  const QStyleOptionViewItem &option,
195  const QPersistentModelIndex &index) const
196  {
197  Q_UNUSED(index);
198  KPushButton *aboutPushButton = static_cast<KPushButton*>(widgets[0]);
199  QSize aboutPushButtonSizeHint = aboutPushButton->sizeHint();
200  aboutPushButton->resize(aboutPushButtonSizeHint);
201  aboutPushButton->move(dependantLayoutValue(option.rect.width() - MARGIN - aboutPushButtonSizeHint.width(), aboutPushButtonSizeHint.width(), option.rect.width()), option.rect.height() / 2 - aboutPushButtonSizeHint.height() / 2);
202  }
203 
204  int dependantLayoutValue(int value, int width, int totalWidth) const
205  {
206  if (itemView()->layoutDirection() == Qt::LeftToRight) {
207  return value;
208  }
209  return totalWidth - width - value;
210  }
211 
212  QFont titleFont(const QFont &baseFont) const
213  {
214  QFont retFont(baseFont);
215  retFont.setBold(true);
216  return retFont;
217  }
218 
219 private Q_SLOTS:
220  void info()
221  {
222  PluginsModel *model = static_cast<PluginsModel*>(itemView()->model());
223  GraphFilePluginInterface* plugin = model->pluginData(focusedIndex());
224  if (plugin) {
225  QPointer<KAboutApplicationDialog> aboutPlugin = new KAboutApplicationDialog(plugin->aboutData(), itemView());
226  aboutPlugin->exec();
227  return;
228  }
229  }
230 private:
231  QPushButton *pushButton;
232 };
233 
234 
235 class PluginsView : public QListView
236 {
237 public:
238  PluginsView(QWidget* parent = 0)
239  :QListView(parent)
240  {
241  setModel(new PluginsModel());
242  setItemDelegate(new LoadedPluginsDelegate(this));
243  setVerticalScrollMode(QListView::ScrollPerPixel);
244  }
245 
246  virtual ~PluginsView()
247  {
248  // explicitly delete the delegate here since otherwise
249  // we get spammed by warnings that the KPushButton we return
250  // in createItemWidgets is deleted before the delegate
251  delete itemDelegate();
252  }
253 
254  virtual QSize sizeHint() const
255  {
256  QSize ret = QListView::sizeHint();
257  ret.setWidth(qMax(ret.width(), sizeHintForColumn(0) + 30));
258  return ret;
259  }
260 };
261 
262 
263 LoadedPluginsDialog::LoadedPluginsDialog(QWidget* parent)
264  : KDialog( parent )
265 {
266  setPlainCaption(i18n("Loaded Plugins"));
267  setButtons(KDialog::Close);
268  setDefaultButton(KDialog::Close);
269 
270  QVBoxLayout* vbox = new QVBoxLayout(mainWidget());
271  KTitleWidget* title = new KTitleWidget(this);
272  title->setText(i18n("Graph File Plugins loaded for %1", KGlobal::mainComponent().aboutData()->programName()));
273  vbox->addWidget(title);
274  vbox->addWidget(new PluginsView());
275 }
276 
277 #include "moc_LoadedPluginsDialog.cpp"
278 #include "LoadedPluginsDialog.moc"
QModelIndex
QWidget
QAbstractItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const =0
QSize::width
int width() const
QAbstractItemView
QFont
QAbstractItemView::setModel
virtual void setModel(QAbstractItemModel *model)
QPointer
QPainter::save
void save()
KDialog
QAbstractItemView::setVerticalScrollMode
void setVerticalScrollMode(ScrollMode mode)
QFontMetrics
sortPlugins
bool sortPlugins(GraphFilePluginInterface *pluginL, GraphFilePluginInterface *pluginR)
Definition: LoadedPluginsDialog.cpp:41
QAbstractScrollArea::sizeHint
virtual QSize sizeHint() const
QListView
QObject::name
const char * name() const
QRect
QModelIndex::isValid
bool isValid() const
QPainter::setFont
void setFont(const QFont &font)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QWidget::layoutDirection
layoutDirection
QStyleOptionViewItem
QObject
QPainter::setPen
void setPen(const QColor &color)
QAbstractListModel
QSize::setWidth
void setWidth(int width)
QPainter
QAbstractItemView::setItemDelegate
void setItemDelegate(QAbstractItemDelegate *delegate)
QModelIndex::row
int row() const
QAbstractItemView::itemDelegate
QAbstractItemDelegate * itemDelegate() const
QVBoxLayout
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QString
QList< GraphFilePluginInterface * >
QModelIndex::parent
QModelIndex parent() const
QAbstractItemModel::parent
virtual QModelIndex parent(const QModelIndex &index) const =0
QSize
QPainter::restore
void restore()
QPersistentModelIndex
QModelIndex::model
const QAbstractItemModel * model() const
MARGIN
#define MARGIN
Definition: LoadedPluginsDialog.cpp:39
QApplication::style
QStyle * style()
QAbstractItemView::sizeHintForColumn
virtual int sizeHintForColumn(int column) const
QSize::height
int height() const
QModelIndex::column
int column() const
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
LoadedPluginsDialog::LoadedPluginsDialog
LoadedPluginsDialog(QWidget *parent=0)
Definition: LoadedPluginsDialog.cpp:263
QPushButton
LoadedPluginsDialog.h
QAbstractItemView::model
QAbstractItemModel * model() const
QObject::parent
QObject * parent() const
QVariant::toString
QString toString() const
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/App

Skip menu "rocs/App"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal