KProperty

cursoredit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2008-2015 Jarosław Staniek <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20 */
21 
22 #include "cursoredit.h"
23 #include "KProperty.h"
24 #include "KPropertyCoreUtils_p.h"
25 #include "KPropertyListData.h"
26 #include "KPropertyUtils_p.h"
27 
28 #include "xpm/blank_cursor.xpm"
29 #include "xpm/arrow_cursor.xpm"
30 #include "xpm/bdiag_cursor.xpm"
31 #include "xpm/busy_cursor.xpm"
32 #include "xpm/closedhand_cursor.xpm"
33 #include "xpm/cross_cursor.xpm"
34 #include "xpm/fdiag_cursor.xpm"
35 #include "xpm/forbidden_cursor.xpm"
36 #include "xpm/hand_cursor.xpm"
37 #include "xpm/ibeam_cursor.xpm"
38 #include "xpm/openhand_cursor.xpm"
39 #include "xpm/sizeall_cursor.xpm"
40 #include "xpm/sizehor_cursor.xpm"
41 #include "xpm/sizever_cursor.xpm"
42 #include "xpm/splith_cursor.xpm"
43 #include "xpm/splitv_cursor.xpm"
44 #include "xpm/uparrow_cursor.xpm"
45 #include "xpm/wait_cursor.xpm"
46 #include "xpm/whatsthis_cursor.xpm"
47 
48 #include <QVariant>
49 #include <QCursor>
50 
51 class CursorListData : public KPropertyListData
52 {
53 public:
54  CursorListData() : KPropertyListData(keysInternal(), stringsInternal())
55  {
56  }
57 
58  Qt::CursorShape indexToShape(int index) const
59  {
60  return static_cast<Qt::CursorShape>(
61  keys().value(index, static_cast<int>(Qt::ArrowCursor)).toInt());
62  }
63 
64  int shapeToIndex(Qt::CursorShape _shape) const
65  {
66  int index = 0;
67  for (const QVariant &shape : keys()) {
68  if (shape.toInt() == _shape)
69  return index;
70  index++;
71  }
72  return 0;
73  }
74 
75  QPixmap pixmapForIndex(int index, const QPalette& pal, bool transparentBackground = false) const
76  {
77  if (index < 0 || index > 18)
78  index = 0;
79  QPixmap xpm(m_xpms[index]);
80  if (transparentBackground)
81  return xpm;
82  QPixmap px(xpm.size());
83  QColor bg( pal.color(QPalette::Base) ); // paint bg with to avoid invisible black-on-black painting
84  bg.setAlpha(127);
85  px.fill(bg);
86  QPainter p(&px);
87  p.drawPixmap(0, 2, xpm);
88  return px;
89  }
90 private:
91  static QList<QVariant> keysInternal() {
93  keys
94  << int(Qt::BlankCursor)
95  << int(Qt::ArrowCursor)
96  << int(Qt::UpArrowCursor)
97  << int(Qt::CrossCursor)
98  << int(Qt::WaitCursor)
99  << int(Qt::IBeamCursor)
100  << int(Qt::SizeVerCursor)
101  << int(Qt::SizeHorCursor)
102  << int(Qt::SizeBDiagCursor)
103  << int(Qt::SizeFDiagCursor)
104  << int(Qt::SizeAllCursor)
105  << int(Qt::SplitVCursor)
106  << int(Qt::SplitHCursor)
107  << int(Qt::PointingHandCursor)
108  << int(Qt::ForbiddenCursor)
109  << int(Qt::WhatsThisCursor)
110  << int(Qt::BusyCursor)
111  << int(Qt::OpenHandCursor)
112  << int(Qt::ClosedHandCursor);
113  return keys;
114  }
115 
116  static QStringList stringsInternal() {
117  QStringList strings;
118  strings << QObject::tr("No cursor", "Mouse Cursor Shape") //0
119  << QObject::tr("Arrow", "Mouse Cursor Shape") //1
120  << QObject::tr("Up arrow", "Mouse Cursor Shape") //2
121  << QObject::tr("Cross", "Mouse Cursor Shape") //3
122  << QObject::tr("Waiting", "Mouse Cursor Shape") //4
123  << QObject::tr("Text cursor", "Mouse Cursor Shape") //5
124  << QObject::tr("Size vertical", "Mouse Cursor Shape") //6
125  << QObject::tr("Size horizontal", "Mouse Cursor Shape") //7
126  << QObject::tr("Size slash", "Mouse Cursor Shape") //8
127  << QObject::tr("Size backslash", "Mouse Cursor Shape") //9
128  << QObject::tr("Size all", "Mouse Cursor Shape") //10
129  << QObject::tr("Split vertical", "Mouse Cursor Shape") //11
130  << QObject::tr("Split horizontal", "Mouse Cursor Shape") //12
131  << QObject::tr("Pointing hand", "Mouse Cursor Shape") //13
132  << QObject::tr("Forbidden", "Mouse Cursor Shape") //14
133  << QObject::tr("What's this?", "Mouse Cursor Shape") //15
134  << QObject::tr("Busy", "Mouse Cursor Shape") //16
135  << QObject::tr("Open hand", "Mouse Cursor Shape") //17
136  << QObject::tr("Closed hand", "Mouse Cursor Shape"); //18
137  return strings;
138  }
139  static const char * const * const m_xpms[];
140 };
141 
142 const char * const * const CursorListData::m_xpms[] =
143 {
144  blank_cursor_xpm,
145  arrow_cursor_xpm,
146  uparrow_cursor_xpm,
147  cross_cursor_xpm,
148  wait_cursor_xpm,
149  ibeam_cursor_xpm,
150  sizever_cursor_xpm,
151  sizehor_cursor_xpm,
152  bdiag_cursor_xpm,
153  fdiag_cursor_xpm,
154  sizeall_cursor_xpm,
155  splitv_cursor_xpm,
156  splith_cursor_xpm,
157  hand_cursor_xpm,
158  forbidden_cursor_xpm,
159  whatsthis_cursor_xpm,
160  busy_cursor_xpm,
161  openhand_cursor_xpm,
162  closedhand_cursor_xpm
163 };
164 
165 Q_GLOBAL_STATIC(CursorListData, s_cursorListData)
166 
167 //----------------------
168 
169 class CursorIconProvider : public KPropertyComboBoxEditorIconProviderInterface
170 {
171 public:
172  explicit CursorIconProvider(QWidget* parent) : m_parent(parent) {}
173  QIcon icon(int index) const override
174  {
175  return s_cursorListData->pixmapForIndex(index, m_parent->palette());
176  }
177  KPropertyComboBoxEditorIconProviderInterface *clone() const override
178  {
179  return new CursorIconProvider(m_parent);
180  }
181  QWidget* m_parent;
182 };
183 
184 //----------------------
185 
186 static KPropertyComboBoxEditorOptions initComboBoxOptions(QWidget* parent)
187 {
188  KPropertyComboBoxEditorOptions options;
189  options.iconProvider = new CursorIconProvider(parent);
190  return options;
191 }
192 
193 class Q_DECL_HIDDEN KPropertyCursorEditor::Private
194 {
195 public:
196  Private()
197  {
198  }
199 };
200 
201 KPropertyCursorEditor::KPropertyCursorEditor(QWidget *parent)
202  : KPropertyComboBoxEditor(*s_cursorListData, initComboBoxOptions( this ), parent),
203  d(new Private)
204 {
205  int paddingTop = 1;
206  int paddingLeft = 2;
207  const QString style(parent->style()->objectName());
208  if (!KPropertyUtilsPrivate::gridLineColor(this).isValid()) {
209  setFrame(false);
210  paddingTop = 0;
211  }
212  if (style == QLatin1String("windowsvista") || style == QLatin1String("fusion")) {
213  paddingLeft = 1;
214  }
215  QString styleSheet = QString::fromLatin1("QComboBox { \
216  %1 \
217  padding-top: %2px; padding-left: %3px; }").arg(KPropertyComboBoxEditor::borderSheet(this))
218  .arg(paddingTop).arg(paddingLeft);
219  setStyleSheet(styleSheet);
220 }
221 
222 KPropertyCursorEditor::~KPropertyCursorEditor()
223 {
224  delete d;
225 }
226 
227 QCursor KPropertyCursorEditor::cursorValue() const
228 {
229  return QCursor((Qt::CursorShape)KPropertyComboBoxEditor::value().toInt());
230 }
231 
232 void KPropertyCursorEditor::setCursorValue(const QCursor &value)
233 {
234  KPropertyComboBoxEditor::setValue( (int)(value.shape()) );
235 }
236 
237 //---------------
238 
239 KPropertyCursorDelegate::KPropertyCursorDelegate()
240 {
241  options()->setBordersVisible(true);
242 }
243 
244 QWidget * KPropertyCursorDelegate::createEditor( int type, QWidget *parent,
245  const QStyleOptionViewItem & option, const QModelIndex & index ) const
246 {
247  Q_UNUSED(type);
248  Q_UNUSED(option);
249  Q_UNUSED(index);
250  return new KPropertyCursorEditor(parent);
251 }
252 
253 void KPropertyCursorDelegate::paint( QPainter * painter,
254  const QStyleOptionViewItem & option, const QModelIndex & index ) const
255 {
256  const KPropertyUtilsPrivate::PainterSaver saver(painter);
257  int comboIndex = s_cursorListData->shapeToIndex( index.data(Qt::EditRole).value<QCursor>().shape() );
258  int pmSize = (option.rect.height() >= 32) ? 32 : 16;
259  const QPixmap pm( s_cursorListData->pixmapForIndex(comboIndex, option.palette)
260  .scaled(pmSize, pmSize, Qt::KeepAspectRatio, Qt::SmoothTransformation) );
261  QPoint pmPoint(option.rect.topLeft() + QPoint(2, 1));
262  painter->drawPixmap(pmPoint, pm);
263  QRect r(option.rect);
264  r.setLeft(7 + r.left() + 1 + pm.width());
265  painter->drawText(r, Qt::AlignVCenter | Qt::AlignLeft, valueToString(index.data(Qt::EditRole), QLocale()));
266 }
267 
268 QString KPropertyCursorDelegate::valueToString(const QVariant& value, const QLocale &locale) const
269 {
270  const Qt::CursorShape shape = value.value<QCursor>().shape();
271  if (locale.language() == QLocale::C) {
272  return KPropertyUtils::keyForEnumValue("CursorShape", shape);
273  }
274  const int comboIndex = s_cursorListData->shapeToIndex(shape);
275  return s_cursorListData->names().value(comboIndex).toString();
276 }
QString styleSheet() const const
QStyle * style() const const
const QColor & color(QPalette::ColorGroup group, QPalette::ColorRole role) const const
T value() const const
void setAlpha(int alpha)
QString tr(const char *sourceText, const char *disambiguation, int n)
AlignVCenter
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
EditRole
CursorShape
void drawText(const QPointF &position, const QString &text)
QLocale::Language language() const const
QLocale locale() const const
QVariant data(int role) const const
KeepAspectRatio
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QVariantList keys() const
A list containing all possible keys for a property.
SmoothTransformation
A data container for properties of list type.
Qt::CursorShape shape() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.