SymbolEditor  1.3.0
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
SymbolListWidget.cpp
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2012 by Stephen Allewell *
3  * sallewell@users.sourceforge.net *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ********************************************************************************/
10 
11 
32 #include "SymbolListWidget.h"
33 
34 #include <QMimeData>
35 #include <QPainter>
36 #include <QPen>
37 
38 #include "Commands.h"
39 #include "Symbol.h"
40 #include "SymbolLibrary.h"
41 
42 
47  : KListWidget(parent),
48  m_library(0),
49  m_lastIndex(0)
50 {
51  setResizeMode(QListView::Adjust);
52  setViewMode(QListView::IconMode);
53  setIconSize(48);
54 }
55 
56 
61 {
62 }
63 
64 
73 {
74  m_size = size;
75  KListWidget::setIconSize(QSize(m_size, m_size));
76  setGridSize(QSize(m_size, m_size));
77  updateIcons();
78 }
79 
80 
88 {
89  if (!library) {
90  return;
91  }
92 
93  m_library = library;
94 
95  foreach (qint16 index, library->indexes()) {
96  addSymbol(index, library->symbol(index));
97  }
98 }
99 
100 
107 void SymbolListWidget::addSymbol(qint16 index, const Symbol &symbol)
108 {
109  QListWidgetItem *item = createItem(index);
110  item->setIcon(createIcon(symbol, m_size));
111 }
112 
113 
120 {
121  if (m_items.contains(index)) {
122  removeItemWidget(m_items.value(index));
123  delete m_items.take(index);
124  }
125 }
126 
127 
138 QListWidgetItem *SymbolListWidget::createItem(qint16 index)
139 {
140  if (m_items.contains(index)) {
141  return m_items.value(index);
142  }
143 
144  QListWidgetItem *item = new QListWidgetItem;
145  item->setData(Qt::UserRole, index);
146  m_items.insert(index, item);
147  int i = index;
148 
149  while (++i < m_lastIndex)
150  if (m_items.contains(i)) {
151  break;
152  }
153 
154  if (i >= m_lastIndex) {
155  addItem(item);
156  m_lastIndex = index;
157  } else {
158  insertItem(row(m_items[i]), item);
159  }
160 
161  return item;
162 }
163 
164 
173 QIcon SymbolListWidget::createIcon(const Symbol &symbol, int size)
174 {
175  QPixmap icon(size, size);
176  icon.fill(Qt::white);
177  QPainter p(&icon);
178  p.setRenderHint(QPainter::Antialiasing, true);
179  p.scale(size, size);
180  QBrush brush(symbol.filled() ? Qt::SolidPattern : Qt::NoBrush);
181  QPen pen;
182 
183  if (!symbol.filled()) {
184  pen.setWidthF(symbol.lineWidth());
185  pen.setCapStyle(symbol.capStyle());
186  pen.setJoinStyle(symbol.joinStyle());
187  }
188 
189  p.setBrush(brush);
190  p.setPen(pen);
191  p.drawPath(symbol.path());
192  p.end();
193  return QIcon(icon);
194 }
195 
196 
202 QStringList SymbolListWidget::mimeTypes() const
203 {
204  QStringList mimetypes;
205  mimetypes.append("application/kxstitchsymbol");
206  return mimetypes;
207 }
208 
209 
211 {
212  return Qt::CopyAction;
213 }
214 
215 
223 QMimeData *SymbolListWidget::mimeData(const QList<QListWidgetItem *> items) const
224 {
225  QMimeData *mimeData = new QMimeData;
226 
227  QByteArray data;
228  QDataStream stream(&data, QIODevice::WriteOnly);
229 
230  foreach (QListWidgetItem * item, items) {
231  qint16 index = static_cast<qint16>(item->data(Qt::UserRole).toInt());
232  Symbol symbol = m_library->symbol(index);
233  stream << symbol;
234  }
235 
236  mimeData->setData("application/kxstitchsymbol", data);
237  return mimeData;
238 }
239 
240 
250 bool SymbolListWidget::dropMimeData(int index, const QMimeData *mimeData, Qt::DropAction action)
251 {
252  Q_UNUSED(action);
253 
254  if (mimeData->hasFormat("application/kxstitchsymbol")) {
255  m_library->undoStack()->push(new DragAndDropCommand(m_library, mimeData));
256  return true;
257  }
258 
259  return false;
260 }
261 
262 
267 {
268  foreach (qint16 index, m_items.keys()) {
269  m_items.value(index)->setIcon(createIcon(m_library->symbol(index), m_size));
270  }
271 }