1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QGraphicsGridLayout>
43 #include <QDebug>
44 
45 #include "recycledlistitem.h"
46 #include "listmodel.h"
47 
48 static const int MinItemHeight = 70;
49 static const int MinItemWidth = 276;
50 
RecycledListItem(QGraphicsWidget * parent)51 RecycledListItem::RecycledListItem(QGraphicsWidget *parent)
52     : AbstractViewItem(parent),
53     m_item(new ListItem(this)),
54     m_item2(0),
55     m_model(0),
56     m_layout(new QGraphicsGridLayout())
57 {
58     m_item->setMinimumWidth(MinItemWidth);
59     setContentsMargins(0,0,0,0);
60     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
61     m_layout->addItem(m_item, 0, 0);
62     setLayout(m_layout);
63     m_layout->setContentsMargins(0,0,0,0);
64     m_layout->setSpacing(0);
65     m_layout->setHorizontalSpacing(0.0);
66     m_layout->setVerticalSpacing(0.0);
67 }
68 
~RecycledListItem()69 RecycledListItem::~RecycledListItem()
70 {
71 }
72 
setModel(QAbstractItemModel * model)73 void RecycledListItem::setModel(QAbstractItemModel *model)
74 {
75     m_model = model;
76 }
77 
78 /*virtual*/
newItemInstance()79 AbstractViewItem *RecycledListItem::newItemInstance()
80 {
81     RecycledListItem* item = new RecycledListItem();
82     return item;
83 }
84 
effectiveSizeHint(Qt::SizeHint which,const QSizeF & constraint) const85 QSizeF RecycledListItem::effectiveSizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 {
87     QSizeF s = m_item->effectiveSizeHint(which,constraint);
88     if (m_item2)
89         s.setWidth(s.width()*2);
90     if (s.height()<MinItemHeight)
91         s.setHeight(MinItemHeight);
92     return s;
93 }
94 
data(int role) const95 QVariant RecycledListItem::data(int role) const
96 {
97     if (m_item && role == Qt::DisplayRole)
98         return m_item->data();
99 
100     return QVariant();
101 }
102 
setData(const QVariant & value,int role)103 void RecycledListItem::setData(const QVariant &value, int role)
104 {
105     if (m_item && role == Qt::DisplayRole) {
106         m_item->setData(value);
107         if (m_item2) {
108             m_item2->setData(value);
109         }
110     }
111 }
112 
113 /*virtual*/
resizeEvent(QGraphicsSceneResizeEvent * event)114 void RecycledListItem::resizeEvent(QGraphicsSceneResizeEvent *event)
115 {
116     AbstractViewItem::resizeEvent(event);
117 }
118 
updateItemContents()119 void RecycledListItem::updateItemContents()
120 {
121     AbstractViewItem::updateItemContents();
122     if (m_model && m_index.isValid())
123         setData(m_model->data(m_index,Qt::DisplayRole), Qt::DisplayRole);
124 }
125 
setTwoColumns(const bool enabled)126 void RecycledListItem::setTwoColumns(const bool enabled)
127 {
128     if (m_item2 && enabled)
129         return;
130 
131     if (enabled) {
132         m_item2 = new ListItem();
133         m_item2->setMinimumWidth(MinItemWidth);
134         m_layout->addItem(m_item2, 0, 1);
135         updateItemContents();
136     }
137     else {
138         if (m_layout->count() > 1) {
139             m_layout->removeAt(1);
140         }
141         delete m_item2;
142         m_item2 = 0;
143     }
144 
145     if (!m_layout->isActivated())
146         m_layout->activate();
147 }
148