2
* UGENE - Integrated Bioinformatics Tools.
3
* Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
4
* http://ugene.unipro.ru
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "QueryPalette.h"
24
#include <U2Lang/QueryDesignerRegistry.h>
26
#include <U2Core/AppContext.h>
28
#include <QtGui/QHeaderView>
29
#include <QtGui/QMouseEvent>
30
#include <QtGui/QApplication>
32
#include <QtGui/QItemDelegate>
33
#include <QtGui/QAction>
38
const QString QDDistanceIds::E2S(QObject::tr("End-Start"));
39
const QString QDDistanceIds::S2E(QObject::tr("Start-End"));
40
const QString QDDistanceIds::E2E(QObject::tr("End-End"));
41
const QString QDDistanceIds::S2S(QObject::tr("Start-Start"));
43
class PaletteDelegate: public QItemDelegate
46
PaletteDelegate(QueryPalette *view) : QItemDelegate(view), m_view(view) {}
48
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
49
const QAbstractItemModel *model = index.model();
52
if (!model->parent(index).isValid()) {
53
// this is a top-level item.
54
QStyleOptionButton buttonOption;
56
buttonOption.state = option.state;
58
buttonOption.state |= QStyle::State_Raised;
60
buttonOption.state &= ~QStyle::State_HasFocus;
62
buttonOption.rect = option.rect;
63
buttonOption.palette = option.palette;
64
buttonOption.features = QStyleOptionButton::None;
65
m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view);
67
QStyleOption branchOption;
68
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
69
QRect r = option.rect;
70
branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
71
branchOption.palette = option.palette;
72
branchOption.state = QStyle::State_Children;
74
if (m_view->isExpanded(index))
75
branchOption.state |= QStyle::State_Open;
77
m_view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, m_view);
80
QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
81
QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
82
model->data(index, Qt::DisplayRole).toString());
83
m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter,
84
option.palette, m_view->isEnabled(), text);
87
QStyleOptionToolButton buttonOption;
88
buttonOption.state = option.state;
89
buttonOption.state &= ~QStyle::State_HasFocus;
90
buttonOption.direction = option.direction;
91
buttonOption.rect = option.rect;
92
buttonOption.font = option.font;
93
buttonOption.fontMetrics = option.fontMetrics;
94
buttonOption.palette = option.palette;
95
buttonOption.subControls = QStyle::SC_ToolButton;
96
buttonOption.features = QStyleOptionToolButton::None;
98
QAction* action = qVariantValue<QAction*>(index.data(Qt::UserRole));
99
buttonOption.text = action->text();
100
buttonOption.icon = action->icon();
101
if (!buttonOption.icon.isNull()) {
102
buttonOption.iconSize = QSize(22, 22);
104
if (action->isChecked()) {
105
buttonOption.state |= QStyle::State_On;
106
buttonOption.state |= QStyle::State_Sunken;
107
buttonOption.activeSubControls = QStyle::SC_ToolButton;
109
buttonOption.state |= QStyle::State_Raised;
110
buttonOption.activeSubControls = QStyle::SC_None;
113
if (m_view->overItem == m_view->itemFromIndex(index)) {
114
buttonOption.state |= QStyle::State_MouseOver;
117
buttonOption.state |= QStyle::State_AutoRaise;
119
buttonOption.toolButtonStyle = Qt::ToolButtonTextBesideIcon;
120
m_view->style()->drawComplexControl(QStyle::CC_ToolButton, &buttonOption, painter, m_view);
122
//QItemDelegate::paint(painter, option, index);
126
virtual QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const {
127
const QAbstractItemModel *model = index.model();
130
QStyleOptionViewItem option = opt;
131
bool top = !model->parent(index).isValid();
132
QSize sz = QItemDelegate::sizeHint(opt, index) + QSize(top?2:20, top?2:20);
136
QueryPalette *m_view;
139
const QString QueryPalette::MIME_TYPE("application/x-ugene-query-id");
141
QueryPalette::QueryPalette(QWidget* parent/* =NULL */)
142
: QTreeWidget(parent), overItem(NULL), currentAction(NULL) {
143
setFocusPolicy(Qt::NoFocus);
144
setSelectionMode(QAbstractItemView::NoSelection);
145
setItemDelegate(new PaletteDelegate(this));
146
setRootIsDecorated(false);
149
header()->setResizeMode(QHeaderView::Stretch);
150
setMouseTracking(true);
152
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored));
155
void QueryPalette::setContent() {
156
QTreeWidgetItem* algorithmCategory = new QTreeWidgetItem;
157
algorithmCategory->setText(0, tr("Algorithms"));
158
addTopLevelItem(algorithmCategory);
159
algorithmCategory->setExpanded(true);
162
QDActorPrototypeRegistry* qpr = AppContext::getQDActorProtoRegistry();
163
foreach(QDActorPrototype* pf, qpr->getAllEntries()) {
164
QAction* action = createItemAction(pf);
165
QTreeWidgetItem* algMenuItem = new QTreeWidgetItem(/*algorithmCategory*/);
166
actionMap[action] = algMenuItem;
167
algMenuItem->setText(0, action->text());
168
algMenuItem->setData(0, Qt::UserRole, qVariantFromValue(action));
169
algorithmCategory->addChild(algMenuItem);
172
QTreeWidgetItem* constraintCategory = new QTreeWidgetItem;
173
constraintCategory->setText(0, tr("Constraints"));
174
addTopLevelItem(constraintCategory);
175
constraintCategory->setExpanded(true);
177
QList<QAction*> constraintItemActions;
178
constraintItemActions << createItemAction(QDDistanceIds::E2S)
179
<< createItemAction(QDDistanceIds::S2E)
180
<< createItemAction(QDDistanceIds::E2E)
181
<< createItemAction(QDDistanceIds::S2S);
183
foreach(QAction* a, constraintItemActions) {
184
QTreeWidgetItem* linkMenuItem = new QTreeWidgetItem(constraintCategory);
185
actionMap[a] = linkMenuItem;
186
linkMenuItem->setText(0, a->text());
187
linkMenuItem->setData(0, Qt::UserRole, qVariantFromValue(a));
188
constraintCategory->addChild(linkMenuItem);
192
QAction* QueryPalette::createItemAction(QDActorPrototype* item) {
193
QAction* a = new QAction(item->getDisplayName(), this);
194
a->setCheckable(true);
195
if (!item->getIcon().isNull()) {
196
a->setIcon(item->getIcon());
199
QIcon icon(":query_designer/images/green_circle.png");
202
a->setData(qVariantFromValue(item));
203
connect(a, SIGNAL(triggered(bool)), SLOT(sl_selectProcess(bool)));
204
connect(a, SIGNAL(toggled(bool)), SLOT(sl_selectProcess(bool)));
208
QAction* QueryPalette::createItemAction(const QString& constraintId) {
209
QAction* a = new QAction(constraintId, this);
210
a->setCheckable(true);
211
QIcon icon(":query_designer/images/green_circle.png");
213
a->setData(qVariantFromValue(constraintId));
214
connect(a, SIGNAL(triggered(bool)), SLOT(sl_selectProcess(bool)));
215
connect(a, SIGNAL(toggled(bool)), SLOT(sl_selectProcess(bool)));
219
void QueryPalette::sl_selectProcess(bool checked) {
220
if (currentAction && currentAction!=sender()) {
221
currentAction->setChecked(false);
224
update(indexFromItem(actionMap.value(currentAction)));
225
currentAction = NULL;
228
currentAction = qobject_cast<QAction*>(sender());
229
assert(currentAction);
232
if (currentAction && currentAction->data().type() != QVariant::String) {
233
emit processSelected(qVariantValue<QDActorPrototype*>(currentAction->data()));
235
emit processSelected(NULL);
239
void QueryPalette::mousePressEvent(QMouseEvent *event) {
240
if (event->buttons() & Qt::LeftButton) {
241
QTreeWidgetItem* item = itemAt(event->pos());
244
if (item->parent() == 0) {
245
setItemExpanded(item, !isItemExpanded(item));
248
QAction* action = qVariantValue<QAction*>(item->data(0, Qt::UserRole));
251
dragStartPosition = event->pos();
252
update(indexFromItem(actionMap.value(action)));
256
//QTreeWidget::mousePressEvent(event);
259
void QueryPalette::mouseMoveEvent(QMouseEvent *event) {
260
if (event->buttons() & Qt::LeftButton) {
261
if ((event->pos()-dragStartPosition).manhattanLength()<QApplication::startDragDistance()) {
264
QPointF pos = event->pos();
265
QTreeWidgetItem* item = itemAt(pos.toPoint());
269
QAction* action = qVariantValue<QAction*>(item->data(0,Qt::UserRole));
274
QDrag *drag = new QDrag(this);
275
QMimeData *mimeData = new QMimeData;
276
if (action->data().type() == QVariant::String) {
277
QString str = action->data().toString();
278
mimeData->setText(str);
280
QDActorPrototype* proto = qVariantValue<QDActorPrototype*>(action->data());
281
mimeData->setText(proto->getId());
284
drag->setMimeData(mimeData);
285
/*Qt::DropAction dropAction = */drag->exec(Qt::CopyAction | Qt::CopyAction);
289
QTreeWidgetItem* prev = overItem;
290
overItem = itemAt(event->pos());
292
update(indexFromItem(prev));
295
update(indexFromItem(overItem));
297
QTreeWidget::mouseMoveEvent(event);
300
void QueryPalette::leaveEvent(QEvent *) {
301
QTreeWidgetItem* prev = overItem;
304
QModelIndex index = indexFromItem(prev);
309
QVariant QueryPalette::saveState() const {
311
for (int i = 0, count = topLevelItemCount(); i < count; i++) {
312
QTreeWidgetItem* it = topLevelItem(i);
313
l.append(it->isExpanded());
318
void QueryPalette::restoreState(const QVariant& v) {
319
const QVariantList& l = v.toList();
320
for(int i=0, n=l.size(); i<n; i++) {
321
topLevelItem(i)->setExpanded(l.at(i).toBool());
b'\\ No newline at end of file'