OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / itemlibrary / itemlibrarycomponents.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "itemlibrarycomponents.h"
35 #include "customdraganddrop.h"
36
37 #include <QtCore/QMimeData>
38 #include <QtCore/QDebug>
39
40 #include <QtGui/QImage>
41 #include <QtGui/QPixmap>
42 #include <QtGui/QDrag>
43 #include <QPainter>
44 #include <QLabel>
45 #include <itemlibraryinfo.h>
46 #include <QDirModel>
47 #include <QProxyStyle>
48
49 enum { debug = 0 };
50
51 namespace QmlDesigner {
52
53 namespace Internal {
54
55 static void drawSelectionBackground(QPainter *painter, const QStyleOption &option)
56 {
57     painter->save();
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());
69     painter->restore();
70 }
71
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
76 {
77 public:
78     void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget * = 0) const
79     {
80         if (element == QStyle::PE_PanelItemViewRow) {
81             if (option->state & QStyle::State_Selected)
82                 drawSelectionBackground(painter, *option);
83         }
84     }
85     int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const {
86         if (hint == SH_ItemView_ShowDecorationSelected)
87             return 0;
88         else
89             return QProxyStyle::styleHint(hint, option, widget, returnData);
90     }
91 };
92
93 ItemLibraryTreeView::ItemLibraryTreeView(QWidget *parent) :
94         QTreeView(parent)
95 {
96     setDragEnabled(true);
97     setDragDropMode(QAbstractItemView::DragOnly);
98     setUniformRowHeights(true);
99     connect(this, SIGNAL(clicked(const QModelIndex &)), this, SLOT(activateItem(const QModelIndex &)));
100     setHeaderHidden(true);
101     setIndentation(20);
102     setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
103     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
104     setAttribute(Qt::WA_MacShowFocusRect, false);
105
106     TreeViewStyle *style = new TreeViewStyle;
107     setStyle(style);
108     style->setParent(this);
109     m_delegate = new ResourceItemDelegate(this);
110     setItemDelegateForColumn(0, m_delegate);
111 }
112
113 // We need to implement startDrag ourselves since we cannot
114 // otherwise influence drag pixmap and hotspot in the standard
115 // implementation.
116 void ItemLibraryTreeView::startDrag(Qt::DropActions /* supportedActions */)
117 {
118     if (debug)
119         qDebug() << Q_FUNC_INFO;
120     QMimeData *mimeData = model()->mimeData(selectedIndexes());
121     if (!mimeData)
122         return;
123
124     QDirModel *dirModel = qobject_cast<QDirModel*>(model());
125     Q_ASSERT(dirModel);
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);
135         drag->exec();
136     }
137 }
138
139 void ItemLibraryTreeView::setModel(QAbstractItemModel *model)
140 {
141     QDirModel *dirModel = dynamic_cast<QDirModel *>(model);
142     if (dirModel) {
143         QTreeView::setModel(model);
144         m_delegate->setModel(dirModel);
145         setColumnHidden(1, true);
146         setColumnHidden(2, true);
147         setColumnHidden(3, true);
148         setSortingEnabled(true);
149     }
150 }
151
152 void ItemLibraryTreeView::activateItem( const QModelIndex & /*index*/)
153 {
154     QMimeData *mimeData = model()->mimeData(selectedIndexes());
155     if (!mimeData)
156         return;
157
158     QString name;
159     QDirModel *dirModel = qobject_cast<QDirModel*>(model());
160     Q_ASSERT(dirModel);
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);
166     }
167 }
168
169 void ResourceItemDelegate::paint(QPainter *painter,
170                const QStyleOptionViewItem &option, const QModelIndex &index) const
171 {
172     if (option.state & QStyle::State_Selected)
173         drawSelectionBackground(painter, option);
174
175     painter->save();
176
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));
181
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);
186
187     painter->drawText(option.rect.bottomLeft()+QPoint(3+pixmap.width(),-8),myString);
188
189     painter->restore();
190 }
191
192 QSize ResourceItemDelegate::sizeHint(const QStyleOptionViewItem &/*option*/,
193                                      const QModelIndex &index) const
194 {
195     QIcon icon(m_model->fileIcon(index));
196     return icon.availableSizes().front() + QSize(25, 4);
197 }
198
199 void ResourceItemDelegate::setModel(QDirModel *model)
200 {
201     m_model = model;
202 }
203
204 } // namespace Internal
205
206 } // namespace QmlDesigner
207