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

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • conf
autopastemodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "autopastemodel.h"
21 #include "settings.h"
22 
23 #include <KComboBox>
24 #include <KIcon>
25 #include <KLineEdit>
26 #include <KLocale>
27 
28 AutoPasteDelegate::AutoPasteDelegate(QAbstractItemModel *types, QAbstractItemModel *syntaxes, QObject *parent)
29  : QStyledItemDelegate(parent),
30  m_types(types),
31  m_syntaxes(syntaxes)
32 {
33 }
34 
35 QWidget *AutoPasteDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
36 {
37  Q_UNUSED(option)
38 
39  if (!index.isValid()) {
40  return 0;
41  }
42 
43  switch(index.column()) {
44  case AutoPasteModel::Type: {
45  KComboBox *types = new KComboBox(parent);
46  types->setModel(m_types);
47  return types;
48  }
49  case AutoPasteModel::Pattern: {
50  KLineEdit *pattern = new KLineEdit(parent);
51  return pattern;
52  }
53  case AutoPasteModel::PatternSyntax: {
54  KComboBox *syntaxes = new KComboBox(parent);
55  syntaxes->setModel(m_syntaxes);
56  return syntaxes;
57  }
58  default:
59  return 0;
60  }
61 }
62 
63 void AutoPasteDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
64 {
65  if (!index.isValid() || !editor) {
66  return;
67  }
68 
69  switch (index.column()) {
70  case AutoPasteModel::Type: {
71  KComboBox *type = static_cast<KComboBox*>(editor);
72  const int row = type->findData(index.data(Qt::EditRole));
73  type->setCurrentIndex(row);
74  break;
75  }
76  case AutoPasteModel::Pattern: {
77  KLineEdit *line = static_cast<KLineEdit*>(editor);
78  line->setText(index.data(Qt::EditRole).toString());
79  break;
80  }
81  case AutoPasteModel::PatternSyntax: {
82  KComboBox *syntax = static_cast<KComboBox*>(editor);
83  const int row = syntax->findData(index.data(Qt::EditRole));
84  syntax->setCurrentIndex(row);
85  break;
86  }
87  default:
88  break;
89  }
90 }
91 
92 void AutoPasteDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
93 {
94  if (!index.isValid() || !editor || !model) {
95  return;
96  }
97 
98  switch (index.column()) {
99  case AutoPasteModel::Type: {
100  KComboBox *typeBox = static_cast<KComboBox*>(editor);
101  const int type = typeBox->itemData(typeBox->currentIndex()).toInt();
102  model->setData(index, type);
103  break;
104  }
105  case AutoPasteModel::Pattern: {
106  KLineEdit *line = static_cast<KLineEdit*>(editor);
107  const QString text = line->text();
108  if (!text.isEmpty()) {
109  model->setData(index, text);
110  }
111  break;
112  }
113  case AutoPasteModel::PatternSyntax: {
114  KComboBox *syntaxBox = static_cast<KComboBox*>(editor);
115  const int syntax = syntaxBox->itemData(syntaxBox->currentIndex()).toInt();
116  model->setData(index, syntax);
117  break;
118  }
119  default:
120  break;
121  }
122 }
123 
124 void AutoPasteDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
125 {
126  Q_UNUSED(index)
127  editor->setGeometry(option.rect);
128 }
129 
130 QSize AutoPasteDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
131 {
132  //make the sizeHint a little bit nicer to have more beautiful editors
133  QSize hint;
134  hint.setWidth(QStyledItemDelegate::sizeHint(option, index).width());
135  hint.setHeight(option.fontMetrics.height() + 7);
136  return hint;
137 }
138 
139 AutoPasteModel::AutoPasteModel(QObject *parent)
140  : QAbstractTableModel(parent)
141 {
142 }
143 
144 AutoPasteModel::~AutoPasteModel()
145 {
146 }
147 
148 int AutoPasteModel::rowCount(const QModelIndex &index) const
149 {
150  if (!index.isValid()) {
151  return m_data.count();
152  }
153 
154  return 0;
155 }
156 
157 int AutoPasteModel::columnCount(const QModelIndex &index) const
158 {
159  if (!index.isValid()) {
160  return 3;
161  }
162 
163  return 0;
164 }
165 
166 QVariant AutoPasteModel::headerData(int section, Qt::Orientation orientation, int role) const
167 {
168  if (orientation == Qt::Vertical) {
169  return QVariant();
170  }
171 
172  if (role == Qt::DisplayRole) {
173  if (section == Pattern) {
174  return i18n("Pattern");
175  } else if (section == PatternSyntax) {
176  return i18n("Syntax");
177  }
178  }
179 
180  return QVariant();
181 }
182 
183 QVariant AutoPasteModel::data(const QModelIndex &index, int role) const
184 {
185  if (!index.isValid()) {
186  return QVariant();
187  }
188 
189  const int column = index.column();
190  const int row = index.row();
191 
192  switch (column) {
193  case Type: {
194  if (role == Qt::DecorationRole) {
195  return (m_data[row].type == Include ? KIcon("list-add") : KIcon("list-remove"));
196  } else if ((role == Qt::UserRole) || (role == Qt::EditRole)) {
197  return m_data[row].type;
198  }
199  break;
200  }
201  case Pattern: {
202  if ((role == Qt::DisplayRole) || (role == Qt::EditRole) || (role == Qt::UserRole)) {
203  return m_data[row].pattern;
204  }
205  break;
206  }
207  case PatternSyntax: {
208  if (role == Qt::DisplayRole) {
209  return (m_data[row].syntax == Wildcard ? i18n("Escape sequences") : i18n("Regular expression"));
210  } else if ((role == Qt::UserRole) || (role == Qt::EditRole)) {
211  return m_data[row].syntax;
212  }
213  break;
214  }
215  default:
216  return QVariant();
217  }
218 
219  return QVariant();
220 }
221 
222 Qt::ItemFlags AutoPasteModel::flags(const QModelIndex &index) const
223 {
224  Q_UNUSED(index)
225 
226  return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
227 }
228 
229 bool AutoPasteModel::setData(const QModelIndex &index, const QVariant &value, int role)
230 {
231  if (role != Qt::EditRole) {
232  return false;
233  }
234 
235  const int row = index.row();
236  const int column = index.column();
237 
238  switch (column) {
239  case Type: {
240  m_data[row].type = static_cast<TypeData>(value.toInt());
241  break;
242  }
243  case Pattern: {
244  m_data[row].pattern = value.toString();
245  break;
246  }
247  case PatternSyntax: {
248  m_data[row].syntax = static_cast<PatternSyntaxData>(value.toInt());
249  break;
250  }
251  default:
252  return false;
253  }
254 
255  emit dataChanged(index, index);
256  return true;
257 }
258 
259 bool AutoPasteModel::removeRows(int row, int count, const QModelIndex &parent)
260 {
261  if (parent.isValid() || (row < 0) || (count < 1) || (row + count > rowCount())) {
262  return false;
263  }
264 
265  beginRemoveRows(parent, row, row + count - 1);
266  while (count) {
267  m_data.removeAt(row);
268  --count;
269  }
270  endRemoveRows();
271 
272  return true;
273 }
274 
275 void AutoPasteModel::addItem(TypeData dataType, PatternSyntaxData patternSyntax, const QString &pattern)
276 {
277  addItems(QList<int>() << dataType, QList<int>() << patternSyntax, QStringList() << pattern);
278 }
279 
280 void AutoPasteModel::addItems(const QList<int> &dataTypes, const QList<int> patternSyntaxes, const QStringList &patterns)
281 {
282  const int row = rowCount();
283  const int numItems = patterns.count();
284  beginInsertRows(QModelIndex(), row, row + numItems - 1);
285 
286  for (int i = 0; i < numItems; ++i) {
287  Data data;
288  data.type = static_cast<TypeData>(dataTypes[i]);
289  data.pattern = patterns[i];
290  data.syntax = static_cast<PatternSyntaxData>(patternSyntaxes[i]);
291  m_data.append(data);
292  }
293 
294  endInsertRows();
295 }
296 
297 bool AutoPasteModel::moveItem(int sourceRow, int destinationRow)
298 {
299  if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow, QModelIndex(), destinationRow)) {
300  return false;
301  }
302 
303  //beginMoveRows asks for different data, than QList::move does, see the 4.7 docs
304  if (sourceRow + 2 == destinationRow) {
305  --destinationRow;
306  }
307  m_data.move(sourceRow, destinationRow);
308  endMoveRows();
309 
310  return true;
311 }
312 
313 void AutoPasteModel::load()
314 {
315  //remove all old items if there are any
316  if (rowCount()) {
317  beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
318  m_data.clear();
319  endRemoveRows();
320  }
321  addItems(Settings::autoPasteTypes(), Settings::autoPastePatternSyntaxes(), Settings::autoPastePatterns());
322 }
323 
324 void AutoPasteModel::save()
325 {
326  QList<int> types;
327  QList<int> syntaxes;
328  QStringList patterns;
329  foreach (const Data &data, m_data) {
330  types << data.type;
331  syntaxes << data.syntax;
332  patterns << data.pattern;
333  }
334 
335  Settings::self()->setAutoPasteTypes(types);
336  Settings::self()->setAutoPastePatternSyntaxes(syntaxes);
337  Settings::self()->setAutoPastePatterns(patterns);
338  Settings::self()->writeConfig();
339 }
340 
341 void AutoPasteModel::resetDefaults()
342 {
343  QStringList names = QStringList() << "AutoPastePatterns" << "AutoPasteTypes" << "AutoPastePatternSyntaxes";
344  foreach (const QString &name, names) {
345  KConfigSkeletonItem *item = Settings::self()->findItem(name);
346  if (item) {
347  item->readDefault(Settings::self()->config());
348  }
349  }
350 
351  load();
352 }
353 
AutoPasteModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: autopastemodel.cpp:229
AutoPasteDelegate::setModelData
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: autopastemodel.cpp:92
AutoPasteModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Definition: autopastemodel.cpp:259
Settings::setAutoPasteTypes
static void setAutoPasteTypes(const QList< int > &v)
Set AutoPasteTypes.
Definition: settings.h:268
AutoPasteModel::PatternSyntaxData
PatternSyntaxData
Definition: autopastemodel.h:58
AutoPasteModel::rowCount
int rowCount(const QModelIndex &index=QModelIndex()) const
Definition: autopastemodel.cpp:148
AutoPasteModel::resetDefaults
void resetDefaults()
Resets the model to the default data.
Definition: autopastemodel.cpp:341
Settings::autoPastePatternSyntaxes
static QList< int > autoPastePatternSyntaxes()
Get AutoPastePatternSyntaxes.
Definition: settings.h:297
AutoPasteModel::~AutoPasteModel
~AutoPasteModel()
Definition: autopastemodel.cpp:144
QWidget
Settings::autoPasteTypes
static QList< int > autoPasteTypes()
Get AutoPasteTypes.
Definition: settings.h:278
QObject
QStyledItemDelegate
AutoPasteModel::TypeData
TypeData
Definition: autopastemodel.h:54
Settings::autoPastePatterns
static QStringList autoPastePatterns()
Get AutoPastePatterns.
Definition: settings.h:259
AutoPasteDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: autopastemodel.cpp:35
Settings::self
static Settings * self()
Definition: settings.cpp:17
AutoPasteDelegate::AutoPasteDelegate
AutoPasteDelegate(QAbstractItemModel *types, QAbstractItemModel *syntaxes, QObject *parent=0)
Definition: autopastemodel.cpp:28
AutoPasteModel::Include
Definition: autopastemodel.h:55
AutoPasteModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: autopastemodel.cpp:222
AutoPasteModel::Wildcard
Definition: autopastemodel.h:59
AutoPasteModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: autopastemodel.cpp:166
Settings::setAutoPastePatterns
static void setAutoPastePatterns(const QStringList &v)
Set AutoPastePatterns.
Definition: settings.h:249
AutoPasteModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: autopastemodel.cpp:183
Settings::setAutoPastePatternSyntaxes
static void setAutoPastePatternSyntaxes(const QList< int > &v)
Set AutoPastePatternSyntaxes.
Definition: settings.h:287
AutoPasteModel::load
void load()
Loads the stored data.
Definition: autopastemodel.cpp:313
QAbstractItemModel
AutoPasteDelegate::setEditorData
void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: autopastemodel.cpp:63
AutoPasteModel::Type
Definition: autopastemodel.h:50
settings.h
AutoPasteModel::columnCount
int columnCount(const QModelIndex &index=QModelIndex()) const
Definition: autopastemodel.cpp:157
AutoPasteDelegate::updateEditorGeometry
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: autopastemodel.cpp:124
AutoPasteModel::Pattern
Definition: autopastemodel.h:51
AutoPasteModel::moveItem
bool moveItem(int sourceRow, int destinationRow)
Moves an item to a new position.
Definition: autopastemodel.cpp:297
AutoPasteModel::PatternSyntax
Definition: autopastemodel.h:52
AutoPasteModel::save
void save()
Saves the current data.
Definition: autopastemodel.cpp:324
autopastemodel.h
AutoPasteModel::addItem
void addItem(TypeData dataType, PatternSyntaxData patternSyntax, const QString &pattern)
Adds an an item.
Definition: autopastemodel.cpp:275
AutoPasteModel::AutoPasteModel
AutoPasteModel(QObject *parent=0)
Definition: autopastemodel.cpp:139
QAbstractTableModel
AutoPasteDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: autopastemodel.cpp:130
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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