KProperty

linestyleedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2010-2015 Jarosław Staniek <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "linestyleedit.h"
21 #include "KPropertyWidgetsFactory.h"
22 #include "combobox.h"
23 #include "KPropertyLineStyleItemDelegate_p.h"
24 #include "KPropertyUtils_p.h"
25 
26 #include <QApplication>
27 #include <QPen>
28 
29 class Q_DECL_HIDDEN KPropertyLineStyleComboEditor::Private
30 {
31 public:
32  Private()
33  {
34  }
35 };
36 
37 KPropertyLineStyleComboEditor::KPropertyLineStyleComboEditor(QWidget *parent)
38  : KPropertyLineStyleSelector(parent), d(new Private)
39 {
40  connect(this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)));
41 
42  int paddingTop = 1;
43  int paddingLeft = 0;
44  const QString style(parent->style()->objectName());
45  if (!KPropertyUtilsPrivate::gridLineColor(this).isValid()) {
46  setFrame(false);
47  paddingTop = 0;
48  }
49  if (style == QLatin1String("windows") || style == QLatin1String("fusion")) {
50  paddingLeft = 3;
51  } else if (style == QLatin1String("windowsvista")) {
52  paddingLeft = 2;
53  }
54  QString styleSheet = QString::fromLatin1("KPropertyLineStyleSelector { \
55  %1 \
56  padding-top: %2px; padding-left: %3px; }").arg(KPropertyComboBoxEditor::borderSheet(this))
57  .arg(paddingTop).arg(paddingLeft);
58  setStyleSheet(styleSheet);
59 }
60 
61 KPropertyLineStyleComboEditor::~KPropertyLineStyleComboEditor()
62 {
63  delete d;
64 }
65 
66 QVariant KPropertyLineStyleComboEditor::value() const
67 {
68  return int(lineStyle());
69 }
70 
71 static bool hasVisibleStyle(const QVariant &value)
72 {
73  return !value.isNull() && value.canConvert(QVariant::Int) && value.toInt() < Qt::CustomDashLine
74  && value.toInt() >= Qt::NoPen;
75 }
76 
77 void KPropertyLineStyleComboEditor::setValue(const QVariant &value)
78 {
79  if (!hasVisibleStyle(value)) {
80  setLineStyle(Qt::NoPen);
81  return;
82  }
83  setLineStyle(static_cast<Qt::PenStyle>(value.toInt()));
84 }
85 
86 void KPropertyLineStyleComboEditor::slotValueChanged(int)
87 {
88  emit commitData(this);
89 }
90 
91 KPropertyLineStyleComboDelegate::KPropertyLineStyleComboDelegate()
92 {
93  options()->setBordersVisible(true);
94 }
95 
96 QWidget * KPropertyLineStyleComboDelegate::createEditor( int type, QWidget *parent,
97  const QStyleOptionViewItem & option, const QModelIndex & index ) const
98 {
99  Q_UNUSED(type)
100  Q_UNUSED(option)
101  Q_UNUSED(index)
102  return new KPropertyLineStyleComboEditor(parent);
103 }
104 
105 void KPropertyLineStyleComboDelegate::paint( QPainter * painter,
106  const QStyleOptionViewItem & option, const QModelIndex & index ) const
107 {
108  const KPropertyUtilsPrivate::PainterSaver saver(painter);
109  Qt::PenStyle penStyle = Qt::NoPen;
110  if (hasVisibleStyle(index.data(Qt::EditRole))) {
111  penStyle = static_cast<Qt::PenStyle>(index.data(Qt::EditRole).toInt());
112  }
113  const QWidget *paintedWidget = dynamic_cast<QWidget*>(painter->device());
114  const QStyle *style = paintedWidget ? paintedWidget->style() : qApp->style();
115  QStyleOptionComboBox cbOption;
116  cbOption.rect = option.rect;
117  QRect r = style->subControlRect(QStyle::CC_ComboBox, &cbOption, QStyle::SC_ComboBoxEditField, nullptr);
118  r.setRight(option.rect.right() - (r.left() - option.rect.left()));
119  KPropertyLineStyleItemDelegate::paintItem(painter, QPen(penStyle), r, option);
120 }
121 
122 QString KPropertyLineStyleComboDelegate::valueToString(const QVariant& value, const QLocale &locale) const
123 {
124  Qt::PenStyle style = (value.isNull() || !value.canConvert(QVariant::Int) || value.toInt() > Qt::CustomDashLine
125  || value.toInt() < Qt::NoPen) ? Qt::NoPen : static_cast<Qt::PenStyle>(value.toInt());
126  return KPropertyLineStyleItemDelegate::styleName(style, locale);
127 }
bool canConvert(int targetTypeId) const const
CustomDashLine
Line style combo box (KProperty::LineStyle, equivalent of Qt::PenStyle)
Definition: linestyleedit.h:30
QStyle * style() const const
virtual QRect subControlRect(QStyle::ComplexControl control, const QStyleOptionComplex *option, QStyle::SubControl subControl, const QWidget *widget) const const =0
int toInt(bool *ok) const const
bool isNull() const const
int left() const const
EditRole
const KPropertyEditorCreatorOptions * options() const
Options for editor creating.
void setRight(int x)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
SC_ComboBoxEditField
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.