~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to part/schema/katecolortreewidget.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE libraries
2
 
   Copyright (C) 2012 Dominik Haumann <dhaumann kde org>
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 version 2 as published by the Free Software Foundation.
7
 
 
8
 
   This library is distributed in the hope that it will be useful,
9
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
   Library General Public License for more details.
12
 
 
13
 
   You should have received a copy of the GNU Library General Public License
14
 
   along with this library; see the file COPYING.LIB.  If not, write to
15
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
 
   Boston, MA 02110-1301, USA.
17
 
*/
18
 
 
19
 
#include "katecolortreewidget.h"
20
 
 
21
 
#include "katecategorydrawer.h"
22
 
 
23
 
#include <QtGui/QStyledItemDelegate>
24
 
#include <QtGui/QPainter>
25
 
#include <QtGui/QHeaderView>
26
 
 
27
 
#include <klocale.h>
28
 
#include <kconfiggroup.h>
29
 
#include <kdebug.h>
30
 
#include <kcolordialog.h>
31
 
#include <kcolorscheme.h>
32
 
#include <kcolorutils.h>
33
 
 
34
 
#include <QDebug>
35
 
#include <QEvent>
36
 
#include <QKeyEvent>
37
 
 
38
 
//BEGIN KateColorTreeItem
39
 
class KateColorTreeItem : public QTreeWidgetItem
40
 
{
41
 
  public:
42
 
    KateColorTreeItem(const KateColorItem& colorItem, QTreeWidgetItem* parent = 0)
43
 
      : QTreeWidgetItem(parent)
44
 
      , m_colorItem(colorItem)
45
 
    {
46
 
      setText(0, m_colorItem.name);
47
 
      if (!colorItem.whatsThis.isEmpty()) {
48
 
        setData(1, Qt::WhatsThisRole, colorItem.whatsThis);
49
 
      }
50
 
      if (!colorItem.useDefault) {
51
 
        setData(2, Qt::ToolTipRole, i18n("Use default color from the KDE color scheme"));
52
 
      }
53
 
    }
54
 
 
55
 
    QColor color() const {
56
 
      return m_colorItem.color;
57
 
    }
58
 
 
59
 
    void setColor(const QColor& c) {
60
 
      m_colorItem.color = c;
61
 
    }
62
 
 
63
 
    QColor defaultColor() const {
64
 
      return m_colorItem.defaultColor;
65
 
    }
66
 
 
67
 
    bool useDefaultColor() const {
68
 
      return m_colorItem.useDefault;
69
 
    }
70
 
 
71
 
    void setUseDefaultColor(bool useDefault) {
72
 
      m_colorItem.useDefault = useDefault;
73
 
      QString tooltip = useDefault ? QString() : i18n("Use default color from the KDE color scheme");
74
 
      setData(2, Qt::ToolTipRole, tooltip);
75
 
    }
76
 
 
77
 
    QString key() {
78
 
      return m_colorItem.key;
79
 
    }
80
 
 
81
 
    KateColorItem colorItem() const {
82
 
      return m_colorItem;
83
 
    }
84
 
 
85
 
  private:
86
 
    KateColorItem m_colorItem;
87
 
};
88
 
//END KateColorTreeItem
89
 
 
90
 
 
91
 
//BEGIN KateColorTreeDelegate
92
 
class KateColorTreeDelegate : public QStyledItemDelegate
93
 
{
94
 
  public:
95
 
    KateColorTreeDelegate(KateColorTreeWidget* widget)
96
 
      : QStyledItemDelegate(widget)
97
 
      , m_tree(widget)
98
 
    {
99
 
    }
100
 
 
101
 
    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
102
 
      QSize sh = QStyledItemDelegate::sizeHint(option, index);
103
 
      if (!index.parent().isValid()) {
104
 
        sh.rheight() += 2 * m_categoryDrawer.leftMargin();
105
 
      } else {
106
 
        sh.rheight() += m_categoryDrawer.leftMargin();
107
 
      }
108
 
      if (index.column() == 0) {
109
 
        sh.rwidth() += m_categoryDrawer.leftMargin();
110
 
      } else if (index.column() == 1) {
111
 
        sh.rwidth() = 150;
112
 
      } else {
113
 
        sh.rwidth() += m_categoryDrawer.leftMargin();
114
 
      }
115
 
 
116
 
      return sh;
117
 
    }
118
 
    
119
 
    QRect fullCategoryRect(const QStyleOptionViewItem& option, const QModelIndex& index) const {
120
 
      QModelIndex i = index;
121
 
      if (i.parent().isValid()) {
122
 
        i = i.parent();
123
 
      }
124
 
 
125
 
      QTreeWidgetItem* item = m_tree->itemFromIndex(i);
126
 
      QRect r = m_tree->visualItemRect(item);
127
 
 
128
 
      // adapt width
129
 
      r.setLeft(m_categoryDrawer.leftMargin());
130
 
      r.setWidth(m_tree->viewport()->width() - m_categoryDrawer.leftMargin() - m_categoryDrawer.rightMargin());
131
 
 
132
 
      // adapt height
133
 
      if (item->isExpanded() && item->childCount() > 0) {
134
 
        const int childCount = item->childCount();
135
 
        const int h = sizeHint(option, index.child(0, 0)).height();
136
 
        r.setHeight(r.height() + childCount * h);
137
 
      }
138
 
 
139
 
      r.setTop(r.top() + m_categoryDrawer.leftMargin());
140
 
 
141
 
      return r;
142
 
    }
143
 
 
144
 
    virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
145
 
    {
146
 
      Q_ASSERT(index.isValid());
147
 
      Q_ASSERT(index.column() >= 0 && index.column() <= 2);
148
 
 
149
 
      //BEGIN: draw toplevel items
150
 
      if (!index.parent().isValid()) {
151
 
        QStyleOptionViewItem opt(option);
152
 
        const QRegion cl = painter->clipRegion();
153
 
        painter->setClipRect(opt.rect);
154
 
        opt.rect = fullCategoryRect(option, index);
155
 
        m_categoryDrawer.drawCategory(index, 0, opt, painter);
156
 
        painter->setClipRegion(cl);
157
 
        return;
158
 
      }
159
 
      //END: draw toplevel items
160
 
 
161
 
      //BEGIN: draw background of category for all other items
162
 
      {
163
 
        QStyleOptionViewItem opt(option);
164
 
        opt.rect = fullCategoryRect(option, index);
165
 
        const QRegion cl = painter->clipRegion();
166
 
        QRect cr = option.rect;
167
 
        if (index.column() == 0) {
168
 
          if (m_tree->layoutDirection() == Qt::LeftToRight) {
169
 
            cr.setLeft(5);
170
 
          } else {
171
 
            cr.setRight(opt.rect.right());
172
 
          }
173
 
        }
174
 
        painter->setClipRect(cr);
175
 
        m_categoryDrawer.drawCategory(index, 0, opt, painter);
176
 
        painter->setClipRegion(cl);
177
 
        painter->setRenderHint(QPainter::Antialiasing, false);
178
 
      }
179
 
      //END: draw background of category for all other items
180
 
 
181
 
      // paint the text 
182
 
      QStyledItemDelegate::paint(painter, option, index);
183
 
      if (index.column() == 0) {
184
 
        return;
185
 
      }
186
 
 
187
 
      painter->setClipRect(option.rect);
188
 
      KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(m_tree->itemFromIndex(index));
189
 
 
190
 
      //BEGIN: draw color button
191
 
      if (index.column() == 1) {
192
 
 
193
 
        QColor color = item->useDefaultColor() ? item->defaultColor() : item->color();
194
 
 
195
 
        QStyleOptionButton opt;
196
 
        opt.rect = option.rect;
197
 
        opt.palette = m_tree->palette();
198
 
 
199
 
        m_tree->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_tree);
200
 
        opt.rect = m_tree->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, m_tree);
201
 
        opt.rect.adjust(1, 1, -1, -1);
202
 
        painter->fillRect(opt.rect, color);
203
 
        qDrawShadePanel(painter, opt.rect, opt.palette, true, 1, NULL);
204
 
      }
205
 
      //END: draw color button
206
 
 
207
 
      //BEGIN: draw reset icon
208
 
      if (index.column() == 2 && !item->useDefaultColor()) {
209
 
 
210
 
        QPixmap p = SmallIcon("edit-undo");
211
 
        QRect rect(option.rect.left() + 10, option.rect.top() + (option.rect.height() - p.height() + 1) / 2, p.width(), p.height());
212
 
 
213
 
        if (option.state & QStyle::State_MouseOver || option.state & QStyle::State_HasFocus) {
214
 
          painter->drawPixmap(rect, p);
215
 
        } else {
216
 
          painter->drawPixmap(rect, SmallIcon("edit-undo", 0, KIconLoader::DisabledState));
217
 
        }
218
 
      }
219
 
      //END: draw reset icon
220
 
    }
221
 
 
222
 
  private:
223
 
    KateColorTreeWidget* m_tree;
224
 
    KateCategoryDrawer m_categoryDrawer;
225
 
};
226
 
//END KateColorTreeDelegate
227
 
 
228
 
KateColorTreeWidget::KateColorTreeWidget(QWidget *parent)
229
 
  : QTreeWidget(parent)
230
 
{
231
 
  setItemDelegate(new KateColorTreeDelegate(this));
232
 
 
233
 
  QStringList headers;
234
 
  headers << QString() // i18nc("@title:column the color name", "Color Role")
235
 
          << QString() // i18nc("@title:column a color button", "Color")
236
 
          << QString();// i18nc("@title:column use default color", "Reset")
237
 
  setHeaderLabels(headers);
238
 
  setHeaderHidden(true);
239
 
  setRootIsDecorated(false);
240
 
  setIndentation(25);
241
 
}
242
 
 
243
 
bool KateColorTreeWidget::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event)
244
 
{
245
 
  // accept edit only for color buttons in column 1 and reset in column 2
246
 
  if (!index.parent().isValid() || index.column() < 1) {
247
 
    return QTreeWidget::edit(index, trigger, event);
248
 
  }
249
 
 
250
 
  bool accept = false;
251
 
  if (event && event->type() == QEvent::KeyPress) {
252
 
    QKeyEvent* ke = static_cast<QKeyEvent*>(event);
253
 
    accept = (ke->key() == Qt::Key_Space); // allow Space to edit
254
 
  }
255
 
 
256
 
  switch (trigger) {
257
 
    case QAbstractItemView::DoubleClicked:
258
 
    case QAbstractItemView::SelectedClicked:
259
 
    case QAbstractItemView::EditKeyPressed: // = F2
260
 
      accept = true;
261
 
      break;
262
 
    default: break;
263
 
  }
264
 
 
265
 
  if (accept) {
266
 
    KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(itemFromIndex(index));
267
 
    QColor color = item->useDefaultColor() ? item->defaultColor() : item->color();
268
 
 
269
 
    if (index.column() == 1) {
270
 
      if (KColorDialog::getColor(color, item->defaultColor(), this) == QDialog::Accepted) {
271
 
        item->setUseDefaultColor(false);
272
 
        item->setColor(color);
273
 
        viewport()->update();
274
 
        emit changed();
275
 
      }
276
 
    } else if (index.column() == 2 && !item->useDefaultColor()) {
277
 
      item->setUseDefaultColor(true);
278
 
      viewport()->update();
279
 
      emit changed();
280
 
    }
281
 
 
282
 
    return false;
283
 
  }
284
 
  return QTreeWidget::edit(index, trigger, event);
285
 
}
286
 
 
287
 
void KateColorTreeWidget::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const
288
 
{
289
 
  Q_UNUSED(painter)
290
 
  Q_UNUSED(rect)
291
 
  Q_UNUSED(index)
292
 
}
293
 
 
294
 
void KateColorTreeWidget::selectDefaults()
295
 
{
296
 
  bool somethingChanged = false;
297
 
 
298
 
  // use default colors for all selected items
299
 
  for (int a = 0; a < topLevelItemCount(); ++a) {
300
 
    QTreeWidgetItem* top = topLevelItem(a);
301
 
    for (int b = 0; b < top->childCount(); ++b) {
302
 
      KateColorTreeItem* it = dynamic_cast<KateColorTreeItem*>(top->child(b));
303
 
      Q_ASSERT(it);
304
 
      if (!it->useDefaultColor()) {
305
 
        it->setUseDefaultColor(true);
306
 
        somethingChanged = true;
307
 
      }
308
 
    }
309
 
  }
310
 
 
311
 
  if (somethingChanged) {
312
 
    viewport()->update();
313
 
    emit changed();
314
 
  }
315
 
}
316
 
 
317
 
void KateColorTreeWidget::addColorItem(const KateColorItem& colorItem)
318
 
{
319
 
  QTreeWidgetItem* categoryItem = 0;
320
 
  for (int i = 0; i < topLevelItemCount(); ++i) {
321
 
    if (topLevelItem(i)->text(0) == colorItem.category) {
322
 
      categoryItem = topLevelItem(i);
323
 
      break;
324
 
    }
325
 
  }
326
 
 
327
 
  if (!categoryItem) {
328
 
    categoryItem = new QTreeWidgetItem();
329
 
    categoryItem->setText(0, colorItem.category);
330
 
    addTopLevelItem(categoryItem);
331
 
    expandItem(categoryItem);
332
 
  }
333
 
 
334
 
  new KateColorTreeItem(colorItem, categoryItem);
335
 
 
336
 
  resizeColumnToContents(0);
337
 
}
338
 
 
339
 
void KateColorTreeWidget::addColorItems(const QVector<KateColorItem>& colorItems)
340
 
{
341
 
  foreach(const KateColorItem& item, colorItems)
342
 
    addColorItem(item);
343
 
}
344
 
 
345
 
QVector<KateColorItem> KateColorTreeWidget::colorItems() const
346
 
{
347
 
  QVector<KateColorItem> items;
348
 
  for (int a = 0; a < topLevelItemCount(); ++a) {
349
 
    QTreeWidgetItem* top = topLevelItem(a);
350
 
    for (int b = 0; b < top->childCount(); ++b) {
351
 
      KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(top->child(b));
352
 
      Q_ASSERT(item);
353
 
      items.append(item->colorItem());
354
 
    }
355
 
  }
356
 
  return items;
357
 
}
358
 
 
359
 
QColor KateColorTreeWidget::findColor(const QString& key) const
360
 
{
361
 
  for (int a = 0; a < topLevelItemCount(); ++a) {
362
 
    QTreeWidgetItem* top = topLevelItem(a);
363
 
    for (int b = 0; b < top->childCount(); ++b) {
364
 
      KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(top->child(b));
365
 
      if (item->key() == key) {
366
 
        if (item->useDefaultColor()) {
367
 
          return item->defaultColor();
368
 
        } else {
369
 
          return item->color();
370
 
        }
371
 
      }
372
 
    }
373
 
  }
374
 
  return QColor();
375
 
}
376
 
 
377
 
// kate: indent-width 2; replace-tabs on;