~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to kde/src/delegates/categorizeddelegate.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 *   Copyright (C) 2009-2014 by Savoir-Faire Linux                          *
 
3
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
 
4
 *                                                                          *
 
5
 *   This library is free software; you can redistribute it and/or          *
 
6
 *   modify it under the terms of the GNU Lesser General Public             *
 
7
 *   License as published by the Free Software Foundation; either           *
 
8
 *   version 2.1 of the License, or (at your option) any later version.     *
 
9
 *                                                                          *
 
10
 *   This library is distributed in the hope that it will be useful,        *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 
13
 *   Lesser General Public License for more details.                        *
 
14
 *                                                                          *
 
15
 *   You should have received a copy of the GNU General Public License      *
 
16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 
17
 ***************************************************************************/
 
18
#include "categorizeddelegate.h"
 
19
 
 
20
//Qt
 
21
#include <QtGui/QTreeView>
 
22
#include <QtGui/QPainter>
 
23
#include <QtGui/QApplication>
 
24
#include <QtCore/QDebug>
 
25
 
 
26
namespace {
 
27
   static const int TOP_MARGIN       = 20;
 
28
   static const int BOTTOM_MARGIN    = 7 ;
 
29
}
 
30
 
 
31
///Construnctor
 
32
CategorizedDelegate::CategorizedDelegate(QTreeView* widget)
 
33
   : QStyledItemDelegate(widget)
 
34
   , m_LeftMargin(7),m_RightMargin(7),m_pChildDelegate(nullptr),m_pChildChildDelegate(nullptr)
 
35
{
 
36
}
 
37
 
 
38
///Destructor
 
39
CategorizedDelegate::~CategorizedDelegate()
 
40
{
 
41
   if ( m_pChildDelegate      ) delete m_pChildDelegate     ;
 
42
   if ( m_pChildChildDelegate ) delete m_pChildChildDelegate;
 
43
}
 
44
 
 
45
///Report category height
 
46
QSize CategorizedDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
 
47
   //Only do it for categories and objects deeper than 1 level, use precalculated values for others
 
48
   if (index.parent().isValid() && !index.parent().parent().isValid() && m_pChildDelegate) {
 
49
      return m_pChildDelegate->sizeHint(option,index);
 
50
   }
 
51
   if (index.parent().parent().isValid() && m_pChildChildDelegate) {
 
52
      return m_pChildChildDelegate->sizeHint(option,index);
 
53
   }
 
54
   if (!index.parent().isValid()) {
 
55
      //This allow deleted contacts and filtered out categories not to be displayed
 
56
      const bool hasChildren = index.child(0,0).isValid();
 
57
      if (!hasChildren)
 
58
         return QSize(0,0);
 
59
 
 
60
      //If the category has children, then return the real size
 
61
      static const int metric = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameVMargin)*2;
 
62
      QSize sh = QStyledItemDelegate::sizeHint(option, index);
 
63
      sh.rheight() += BOTTOM_MARGIN + (index.row()==0?metric:TOP_MARGIN);
 
64
      sh.rwidth() += m_LeftMargin;
 
65
      return sh;
 
66
   }
 
67
   return QStyledItemDelegate::sizeHint(option,index);
 
68
} //sizeHint
 
69
 
 
70
///Draw the category (and the first child)
 
71
void CategorizedDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
 
72
{
 
73
   Q_ASSERT(index.isValid());
 
74
 
 
75
   if (!option.rect.height())
 
76
      return;
 
77
   if (!index.parent().isValid()) {
 
78
      QStyleOptionViewItem opt(option);
 
79
      const QRegion cl = painter->clipRegion();
 
80
      painter->setClipRect(opt.rect);
 
81
      drawSimpleCategory(index, 0, opt, painter);
 
82
      painter->setClipRegion(cl);
 
83
      return;
 
84
   }
 
85
 
 
86
   painter->setClipRect(option.rect);
 
87
 
 
88
   if (m_pChildDelegate && !index.parent().parent().isValid())
 
89
      m_pChildDelegate->paint(painter,option,index);
 
90
   else if (m_pChildChildDelegate && index.parent().parent().isValid())
 
91
      m_pChildChildDelegate->paint(painter,option,index);
 
92
} //paint
 
93
 
 
94
///Set the delagate that are categorized
 
95
void CategorizedDelegate::setChildDelegate(QStyledItemDelegate* childDelegate)
 
96
{
 
97
   m_pChildDelegate = childDelegate;
 
98
}
 
99
 
 
100
///Set the categorized delegate own children
 
101
void CategorizedDelegate::setChildChildDelegate(QStyledItemDelegate* childDelegate)
 
102
{
 
103
   m_pChildChildDelegate = childDelegate;
 
104
}
 
105
 
 
106
void CategorizedDelegate::drawSimpleCategory(const QModelIndex &index, int sortRole, const QStyleOption &option, QPainter *painter) const
 
107
{
 
108
   Q_UNUSED(sortRole)
 
109
   //Some theme have internal glow (such as Oxygen)
 
110
   static const int metric = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameVMargin)*2;
 
111
 
 
112
   const QString category = index.model()->data(index, Qt::DisplayRole).toString();
 
113
   painter->setClipping(false);
 
114
   static QPen pen(QApplication::palette().color(QPalette::Disabled,QPalette::Text));
 
115
   pen.setWidth(1);
 
116
   painter->setPen(pen);
 
117
   painter->setOpacity(0.3);
 
118
   const int topMargin2 = index.row()==0?metric:TOP_MARGIN;
 
119
   painter->drawLine(0,option.rect.y()+(topMargin2),option.rect.x()+option.rect.width()+option.rect.x(),option.rect.y()+(topMargin2));
 
120
   painter->setOpacity(1);
 
121
   painter->drawText(QRect(metric?1.5*metric:4,option.rect.y()+1+(topMargin2),option.rect.width(),option.rect.height()-1),Qt::AlignLeft | Qt::AlignTop,category);
 
122
}