[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfstylemanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfstylemanager.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist@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: klfstylemanager.cpp 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <QApplication>
28 #include <QList>
29 #include <QPushButton>
30 #include <QMessageBox>
31 #include <QInputDialog>
32 #include <QDrag>
33 #include <QMimeData>
34 
35 #include <ui_klfstylemanager.h>
36 
37 #include "klfstylemanager.h"
38 
39 
40 
41 Qt::ItemFlags KLFStyleListModel::flags(const QModelIndex& index) const
42 {
43  if (!index.isValid() || index.row() >= rowCount() || index.model() != this)
44  return Qt::ItemIsDropEnabled; // we allow drops outside the items
45  return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
46 }
47 
49 {
50  return data(index(row), Qt::DisplayRole).toString();
51 }
52 
53 void KLFStyleListModel::setStyleName(int row, const QString& newname)
54 {
55  setData(index(row), newname, Qt::DisplayRole);
56 }
57 
58 
60 {
61  return Qt::CopyAction;
62 }
63 
65 {
66  QStringList types;
67  types << "application/x-klf-stylename";
68  return types;
69 }
70 
71 QMimeData *KLFStyleListModel::mimeData(const QModelIndexList& indexes) const
72 {
73  QMimeData *mimeData = new QMimeData();
74 
75  // can only drag ONE stylename
76  if (indexes.size() > 1 || indexes.size() <= 0) {
77  return mimeData;
78  }
79 
80  QByteArray encodedData;
81  QDataStream stream(&encodedData, QIODevice::WriteOnly);
82  stream << styleName(indexes[0].row());
83  mimeData->setData("application/x-klf-stylename", encodedData);
84  return mimeData;
85 }
86 
87 bool KLFStyleListModel::dropMimeData(const QMimeData *mdata, Qt::DropAction action, int row, int column,
88  const QModelIndex &parent)
89 {
90  if (action == Qt::IgnoreAction)
91  return true;
92 
93  if (parent.isValid())
94  return false;
95 
96  if (!mdata->hasFormat("application/x-klf-stylename"))
97  return false;
98 
99  if (column > 0)
100  return false;
101 
102  if (row == -1)
103  row = rowCount();
104 
105  QByteArray encodedData = mdata->data("application/x-klf-stylename");
106  QDataStream stream(&encodedData, QIODevice::ReadOnly);
107  QString newItem;
108 
109  stream >> newItem;
110 
111  // find style already existant in this list
112  int k;
113  for (k = 0; k < rowCount() && styleName(k) != newItem; ++k)
114  ;
115  if (k >= rowCount()) {
116  fprintf(stderr, "WARNING: Ignoring drop of style named `%s' which was not already in list!\n",
117  newItem.toLocal8Bit().constData());
118  return false;
119  }
120  // remove row at position k
121  removeRows(k, 1);
122  if (row > k)
123  --row;
124  // and insert our text at the right position
125  insertRows(row, 1);
126  setStyleName(row, newItem);
127 
128  emit internalMoveCompleted(k, row);
129 
130  return true;
131 }
132 
133 
134 // ------------------
135 
136 
137 
139  : QWidget(parent, Qt::Dialog)
140 {
141  u = new Ui::KLFStyleManager;
142  u->setupUi(this);
143  setObjectName("KLFStyleManager");
144 
145  _styptr = stydata;
146 
147  _drag_item = 0;
148  _drag_init_pos = QPoint(-1,-1);
149  /* mDropIndicatorItem = 0; */
150 
151  mActionsPopup = new QMenu(this);
152 
155  actPopupDelete = mActionsPopup->addAction("", this, SLOT(slotDelete()));
156  actPopupMoveUp = mActionsPopup->addAction("", this, SLOT(slotMoveUp()));
157  actPopupMoveDown = mActionsPopup->addAction("", this, SLOT(slotMoveDown()));
158  actPopupRename = mActionsPopup->addAction("", this, SLOT(slotRename()));
159  u->btnActions->setMenu(mActionsPopup);
160 
161  mStyleListModel = new KLFStyleListModel(this);
162  u->lstStyles->setModel(mStyleListModel);
163 
164  // populate style list
165  slotRefresh();
166 
167  // and set menu items enabled or not
169 
170  connect(u->btnClose, SIGNAL(clicked()), this, SLOT(hide()));
171 
172  u->lstStyles->installEventFilter(this);
173  connect(u->lstStyles, SIGNAL(customContextMenuRequested(const QPoint&)),
174  this, SLOT(showActionsContextMenu(const QPoint&)));
175  connect(mStyleListModel, SIGNAL(internalMoveCompleted(int, int)),
176  this, SLOT(slotModelMoveCompleted(int, int)));
177  connect(u->lstStyles->selectionModel(),
178  SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
179  this, SLOT(refreshActionsEnabledState()));
180 
181  retranslateUi(false);
182 }
183 
184 
185 void KLFStyleManager::retranslateUi(bool alsoBaseUi)
186 {
188  if (alsoBaseUi)
189  u->retranslateUi(this);
190 
191  actPopupDelete->setText(tr("Delete Style"));
192  actPopupMoveUp->setText(tr("Move up"));
193  actPopupMoveDown->setText(tr("Move down"));
194  actPopupRename->setText(tr("Rename style"));
195 }
196 
198 {
199 }
200 
202 {
203  int curidx = currentRow();
204 
205  if (curidx != -1) {
206  actPopupDelete->setEnabled(true);
207  actPopupRename->setEnabled(true);
208  actPopupMoveUp->setEnabled(curidx > 0);
209  actPopupMoveDown->setEnabled(curidx < mStyleListModel->rowCount()-1);
210  } else {
211  actPopupDelete->setEnabled(false);
212  actPopupRename->setEnabled(false);
213  actPopupMoveUp->setEnabled(false);
214  actPopupMoveDown->setEnabled(false);
215  }
216 }
217 
219 {
220  mActionsPopup->exec(u->lstStyles->mapToGlobal(pos));
221 }
222 
223 
224 int KLFStyleManager::currentRow()
225 {
226  QModelIndexList sel = u->lstStyles->selectionModel()->selectedRows();
227  if (sel.size() == 0)
228  return -1;
229  if (sel.size() >= 2) {
230  qWarning("Multiple style names selected! Expected Single Selection Policy!\n");
231  return -1;
232  }
233  return sel[0].row();
234 }
235 
237 {
238  int r = currentRow();
239  if ( r == -1 )
240  return;
241 
242  if ( QMessageBox::question(this, tr("Erase style?"), tr("Are you sure you want to erase selected style?"),
243  QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes ) {
244  _styptr->removeAt(r);
245  mStyleListModel->removeRows(r, 1);
246  }
247 
248  emit refreshStyles();
250 }
251 
253 {
254  int r = currentRow();
255  if ( r == -1 )
256  return;
257 
258  QString newname = QInputDialog::getText(this, tr("Rename style"), tr("Enter new style name:"), QLineEdit::Normal,
259  _styptr->at(r).name);
260 
261  if ( ! newname.isEmpty() ) {
262  _styptr->operator[](r).name = newname;
263  mStyleListModel->setStyleName(r, newname);
264  }
265 
266  emit refreshStyles();
268 }
269 
271 {
272  int r = currentRow();
273  if ( r < 1 || r >= mStyleListModel->rowCount() )
274  return;
275 
276  QString s = mStyleListModel->styleName(r);
277  mStyleListModel->setStyleName(r, mStyleListModel->styleName(r-1));
278  mStyleListModel->setStyleName(r-1, s);
279  slotModelMoveCompleted(r, r-1);
280 
281  emit refreshStyles();
283 }
284 
286 {
287  int r = currentRow();
288  if ( r < 0 || r > mStyleListModel->rowCount() - 1 )
289  return;
290 
291  QString s = mStyleListModel->styleName(r);
292  mStyleListModel->setStyleName(r, mStyleListModel->styleName(r+1));
293  mStyleListModel->setStyleName(r+1, s);
294  slotModelMoveCompleted(r, r+1);
295 
296  emit refreshStyles();
298 }
299 
300 
301 void KLFStyleManager::slotModelMoveCompleted(int prev, int newpos)
302 {
303  KLFStyle sty = _styptr->takeAt(prev);
304  _styptr->insert(newpos, sty);
305 
306  QModelIndex i = mStyleListModel->index(newpos);
307  u->lstStyles->selectionModel()->select(i, QItemSelectionModel::ClearAndSelect);
308  u->lstStyles->setCurrentIndex(i);
309 
310  emit refreshStyles();
312 }
313 
315 {
316  QStringList list;
317  for (int i = 0; i < _styptr->size(); ++i) {
318  list << _styptr->at(i).name;
319  }
320  mStyleListModel->setStringList(list);
321 }
322 
323 
324 
325 
customContextMenuRequested(const QPoint &pos)
void refreshActionsEnabledState()
data(const QString &mimeType)
Qt::DropActions supportedDropActions() const
QStringList mimeTypes() const
void showActionsContextMenu(const QPoint &pos)
hasFormat(const QString &mimeType)
at(int i)
removeAt(int i)
QMimeData * mimeData(const QModelIndexList &indexes) const
takeAt(int i)
setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
addAction(const QString &text)
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
rowCount(const QModelIndex &parent=QModelIndex()
removeRows(int row, int count, const QModelIndex &parent=QModelIndex()
data(const QModelIndex &index, int role)
question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
void retranslateUi(bool alsoBaseUi=true)
insertRows(int row, int count, const QModelIndex &parent=QModelIndex()
virtual void setStyleName(int row, const QString &newname)
virtual QString styleName(int row) const
#define KLF_DEBUG_TIME_BLOCK(msg)
getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode=QLineEdit::Normal, const QString &text=QString()
#define KLF_FUNC_NAME
void slotModelMoveCompleted(int previouspos, int newpos)
void internalMoveCompleted(int prevrow, int newrow)
insert(int i, const T &value)
void refreshStyles()
setData(const QString &mimeType, const QByteArray &data)
KLFStyleManager(KLFStyleList *ptr, QWidget *parent)
setStringList(const QStringList &strings)

Generated by doxygen 1.8.6