1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "itemlibrarycomponents.h"
35 #include "customdraganddrop.h"
37 #include <QtCore/QMimeData>
38 #include <QtCore/QDebug>
40 #include <QtGui/QImage>
41 #include <QtGui/QPixmap>
42 #include <QtGui/QDrag>
45 #include <itemlibraryinfo.h>
47 #include <QProxyStyle>
51 namespace QmlDesigner {
55 static void drawSelectionBackground(QPainter *painter, const QStyleOption &option)
58 QLinearGradient gradient;
59 QColor highlight = option.palette.highlight().color();
60 gradient.setColorAt(0, highlight.lighter(130));
61 gradient.setColorAt(1, highlight.darker(130));
62 gradient.setStart(option.rect.topLeft());
63 gradient.setFinalStop(option.rect.bottomLeft());
64 painter->fillRect(option.rect, gradient);
65 painter->setPen(highlight.lighter());
66 painter->drawLine(option.rect.topLeft(),option.rect.topRight());
67 painter->setPen(highlight.darker());
68 painter->drawLine(option.rect.bottomLeft(),option.rect.bottomRight());
72 // This style basically allows us to span the entire row
73 // including the arrow indicators which would otherwise not be
74 // drawn by the delegate
75 class TreeViewStyle : public QProxyStyle
78 void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget * = 0) const
80 if (element == QStyle::PE_PanelItemViewRow) {
81 if (option->state & QStyle::State_Selected)
82 drawSelectionBackground(painter, *option);
85 int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const {
86 if (hint == SH_ItemView_ShowDecorationSelected)
89 return QProxyStyle::styleHint(hint, option, widget, returnData);
93 ItemLibraryTreeView::ItemLibraryTreeView(QWidget *parent) :
97 setDragDropMode(QAbstractItemView::DragOnly);
98 setUniformRowHeights(true);
99 connect(this, SIGNAL(clicked(const QModelIndex &)), this, SLOT(activateItem(const QModelIndex &)));
100 setHeaderHidden(true);
102 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
103 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
104 setAttribute(Qt::WA_MacShowFocusRect, false);
106 TreeViewStyle *style = new TreeViewStyle;
108 style->setParent(this);
109 m_delegate = new ResourceItemDelegate(this);
110 setItemDelegateForColumn(0, m_delegate);
113 // We need to implement startDrag ourselves since we cannot
114 // otherwise influence drag pixmap and hotspot in the standard
116 void ItemLibraryTreeView::startDrag(Qt::DropActions /* supportedActions */)
119 qDebug() << Q_FUNC_INFO;
120 QMimeData *mimeData = model()->mimeData(selectedIndexes());
124 QDirModel *dirModel = qobject_cast<QDirModel*>(model());
126 QFileInfo fileInfo = dirModel->fileInfo(selectedIndexes().front());
127 QPixmap pixmap(fileInfo.absoluteFilePath());
128 if (!pixmap.isNull()) {
129 CustomItemLibraryDrag *drag = new CustomItemLibraryDrag(this);
130 drag->setPreview(pixmap);
131 drag->setPixmap(QIcon(pixmap).pixmap(128, 128));
132 QMimeData *mimeData = new QMimeData;
133 mimeData->setData("application/vnd.bauhaus.libraryresource", fileInfo.absoluteFilePath().toLatin1());
134 drag->setMimeData(mimeData);
139 void ItemLibraryTreeView::setModel(QAbstractItemModel *model)
141 QDirModel *dirModel = dynamic_cast<QDirModel *>(model);
143 QTreeView::setModel(model);
144 m_delegate->setModel(dirModel);
145 setColumnHidden(1, true);
146 setColumnHidden(2, true);
147 setColumnHidden(3, true);
148 setSortingEnabled(true);
152 void ItemLibraryTreeView::activateItem( const QModelIndex & /*index*/)
154 QMimeData *mimeData = model()->mimeData(selectedIndexes());
159 QDirModel *dirModel = qobject_cast<QDirModel*>(model());
161 QFileInfo fileInfo = dirModel->fileInfo(selectedIndexes().front());
162 QPixmap pixmap(fileInfo.absoluteFilePath());
163 if (!pixmap.isNull()) {
164 name = "image^" + fileInfo.absoluteFilePath();
165 emit itemActivated(name);
169 void ResourceItemDelegate::paint(QPainter *painter,
170 const QStyleOptionViewItem &option, const QModelIndex &index) const
172 if (option.state & QStyle::State_Selected)
173 drawSelectionBackground(painter, option);
177 QIcon icon(m_model->fileIcon(index));
178 QPixmap pixmap(icon.pixmap(icon.availableSizes().front()));
179 painter->drawPixmap(option.rect.x(),option.rect.y()+2,pixmap);
180 QString myString(m_model->fileName(index));
182 // Check text length does not exceed available space
183 int extraSpace=12+pixmap.width();
184 QFontMetrics fm(option.font);
185 myString = fm.elidedText(myString,Qt::ElideMiddle,option.rect.width()-extraSpace);
187 painter->drawText(option.rect.bottomLeft()+QPoint(3+pixmap.width(),-8),myString);
192 QSize ResourceItemDelegate::sizeHint(const QStyleOptionViewItem &/*option*/,
193 const QModelIndex &index) const
195 QIcon icon(m_model->fileIcon(index));
196 return icon.availableSizes().front() + QSize(25, 4);
199 void ResourceItemDelegate::setModel(QDirModel *model)
204 } // namespace Internal
206 } // namespace QmlDesigner