~ubuntu-branches/debian/jessie/ugene/jessie

« back to all changes in this revision

Viewing changes to src/plugins/query_designer/src/QueryPalette.cpp

  • Committer: Package Import Robot
  • Author(s): Steffen Moeller
  • Date: 2011-11-02 13:29:07 UTC
  • mfrom: (1.2.1) (3.1.11 natty)
  • Revision ID: package-import@ubuntu.com-20111102132907-o34gwnt0uj5g6hen
Tags: 1.9.8+repack-1
* First release to Debian
  - added README.Debian
  - increased policy version to 3.9.2
  - added URLs for version control system
* Added debug package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * UGENE - Integrated Bioinformatics Tools.
 
3
 * Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
 
4
 * http://ugene.unipro.ru
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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,
 
19
 * MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include "QueryPalette.h"
 
23
 
 
24
#include <U2Lang/QueryDesignerRegistry.h>
 
25
 
 
26
#include <U2Core/AppContext.h>
 
27
 
 
28
#include <QtGui/QHeaderView>
 
29
#include <QtGui/QMouseEvent>
 
30
#include <QtGui/QApplication>
 
31
 
 
32
#include <QtGui/QItemDelegate>
 
33
#include <QtGui/QAction>
 
34
 
 
35
 
 
36
namespace U2 {
 
37
 
 
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"));
 
42
 
 
43
class PaletteDelegate: public QItemDelegate
 
44
{
 
45
public:
 
46
    PaletteDelegate(QueryPalette *view) : QItemDelegate(view), m_view(view) {}
 
47
 
 
48
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
 
49
        const QAbstractItemModel *model = index.model();
 
50
        Q_ASSERT(model);
 
51
 
 
52
        if (!model->parent(index).isValid()) {
 
53
            // this is a top-level item.
 
54
            QStyleOptionButton buttonOption;
 
55
 
 
56
            buttonOption.state = option.state;
 
57
#ifdef Q_WS_MAC
 
58
            buttonOption.state |= QStyle::State_Raised;
 
59
#endif
 
60
            buttonOption.state &= ~QStyle::State_HasFocus;
 
61
 
 
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);
 
66
 
 
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;
 
73
 
 
74
            if (m_view->isExpanded(index))
 
75
                branchOption.state |= QStyle::State_Open;
 
76
 
 
77
            m_view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, m_view);
 
78
 
 
79
            // draw text
 
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);
 
85
 
 
86
        } else {
 
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;
 
97
 
 
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);
 
103
            }
 
104
            if (action->isChecked()) {
 
105
                buttonOption.state |= QStyle::State_On;
 
106
                buttonOption.state |= QStyle::State_Sunken;
 
107
                buttonOption.activeSubControls = QStyle::SC_ToolButton;
 
108
            } else {
 
109
                buttonOption.state |= QStyle::State_Raised;
 
110
                buttonOption.activeSubControls = QStyle::SC_None;
 
111
            }
 
112
 
 
113
            if (m_view->overItem == m_view->itemFromIndex(index)) {
 
114
                buttonOption.state |= QStyle::State_MouseOver;
 
115
            }
 
116
 
 
117
            buttonOption.state |= QStyle::State_AutoRaise;
 
118
 
 
119
            buttonOption.toolButtonStyle = Qt::ToolButtonTextBesideIcon;
 
120
            m_view->style()->drawComplexControl(QStyle::CC_ToolButton, &buttonOption, painter, m_view);
 
121
 
 
122
            //QItemDelegate::paint(painter, option, index);
 
123
        }
 
124
    }
 
125
 
 
126
    virtual QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const {
 
127
        const QAbstractItemModel *model = index.model();
 
128
        Q_ASSERT(model);
 
129
 
 
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);
 
133
        return sz;
 
134
    }
 
135
private:
 
136
    QueryPalette *m_view;
 
137
};
 
138
 
 
139
const QString QueryPalette::MIME_TYPE("application/x-ugene-query-id");
 
140
    
 
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);
 
147
    setColumnCount(1);
 
148
    header()->hide();
 
149
    header()->setResizeMode(QHeaderView::Stretch);
 
150
    setMouseTracking(true);
 
151
    setContent();
 
152
    setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored));
 
153
}
 
154
 
 
155
void QueryPalette::setContent() {
 
156
    QTreeWidgetItem* algorithmCategory = new QTreeWidgetItem;
 
157
    algorithmCategory->setText(0, tr("Algorithms"));
 
158
    addTopLevelItem(algorithmCategory);
 
159
    algorithmCategory->setExpanded(true);
 
160
 
 
161
 
 
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);
 
170
    }
 
171
 
 
172
    QTreeWidgetItem* constraintCategory = new QTreeWidgetItem;
 
173
    constraintCategory->setText(0, tr("Constraints"));
 
174
    addTopLevelItem(constraintCategory);
 
175
    constraintCategory->setExpanded(true);
 
176
    
 
177
    QList<QAction*> constraintItemActions;
 
178
    constraintItemActions << createItemAction(QDDistanceIds::E2S)
 
179
        << createItemAction(QDDistanceIds::S2E)
 
180
        << createItemAction(QDDistanceIds::E2E)
 
181
        << createItemAction(QDDistanceIds::S2S);
 
182
 
 
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);
 
189
    }
 
190
}
 
191
 
 
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());
 
197
    }
 
198
    else {
 
199
        QIcon icon(":query_designer/images/green_circle.png");
 
200
        a->setIcon(icon);
 
201
    }
 
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)));
 
205
    return a;
 
206
}
 
207
 
 
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");
 
212
    a->setIcon(icon);
 
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)));
 
216
    return a;
 
217
}
 
218
 
 
219
void QueryPalette::sl_selectProcess(bool checked) {
 
220
    if (currentAction && currentAction!=sender()) {
 
221
        currentAction->setChecked(false);
 
222
    }
 
223
    if (!checked) {
 
224
        update(indexFromItem(actionMap.value(currentAction)));
 
225
        currentAction = NULL;
 
226
    }
 
227
    else {
 
228
        currentAction = qobject_cast<QAction*>(sender());
 
229
        assert(currentAction);
 
230
    }
 
231
 
 
232
    if (currentAction && currentAction->data().type() != QVariant::String) {
 
233
        emit processSelected(qVariantValue<QDActorPrototype*>(currentAction->data()));
 
234
    } else {
 
235
        emit processSelected(NULL);
 
236
    }
 
237
}
 
238
 
 
239
void QueryPalette::mousePressEvent(QMouseEvent *event) {
 
240
    if (event->buttons() & Qt::LeftButton) {
 
241
        QTreeWidgetItem* item = itemAt(event->pos());
 
242
        if (!item) return;
 
243
        event->accept();
 
244
        if (item->parent() == 0) {
 
245
            setItemExpanded(item, !isItemExpanded(item));
 
246
            return;
 
247
        }
 
248
        QAction* action = qVariantValue<QAction*>(item->data(0, Qt::UserRole));
 
249
        if (action) {
 
250
            action->toggle();
 
251
            dragStartPosition = event->pos();
 
252
            update(indexFromItem(actionMap.value(action)));
 
253
        }
 
254
        return;
 
255
    }
 
256
    //QTreeWidget::mousePressEvent(event);
 
257
}
 
258
 
 
259
void QueryPalette::mouseMoveEvent(QMouseEvent *event) {
 
260
    if (event->buttons() & Qt::LeftButton) {
 
261
        if ((event->pos()-dragStartPosition).manhattanLength()<QApplication::startDragDistance()) {
 
262
            return;
 
263
        }
 
264
        QPointF pos = event->pos();
 
265
        QTreeWidgetItem* item = itemAt(pos.toPoint());
 
266
        if(!item) {
 
267
            return;
 
268
        }
 
269
        QAction* action = qVariantValue<QAction*>(item->data(0,Qt::UserRole));
 
270
        if (!action) {
 
271
            return;
 
272
        }
 
273
 
 
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);
 
279
        } else {
 
280
            QDActorPrototype* proto = qVariantValue<QDActorPrototype*>(action->data());
 
281
            mimeData->setText(proto->getId());
 
282
        }
 
283
        
 
284
        drag->setMimeData(mimeData);
 
285
        /*Qt::DropAction dropAction = */drag->exec(Qt::CopyAction | Qt::CopyAction);
 
286
        return;
 
287
    }
 
288
 
 
289
    QTreeWidgetItem* prev = overItem;
 
290
    overItem = itemAt(event->pos());
 
291
    if (prev) {
 
292
        update(indexFromItem(prev));
 
293
    }
 
294
    if (overItem) {
 
295
        update(indexFromItem(overItem));
 
296
    }
 
297
    QTreeWidget::mouseMoveEvent(event);
 
298
}
 
299
 
 
300
void QueryPalette::leaveEvent(QEvent *) {
 
301
    QTreeWidgetItem* prev = overItem;
 
302
    overItem = NULL;
 
303
    if (prev) {
 
304
        QModelIndex index = indexFromItem(prev);
 
305
        update(index);
 
306
    };
 
307
}
 
308
 
 
309
QVariant QueryPalette::saveState() const {
 
310
    QVariantList l;
 
311
    for (int i = 0, count = topLevelItemCount(); i < count; i++) {
 
312
        QTreeWidgetItem* it = topLevelItem(i);
 
313
        l.append(it->isExpanded());
 
314
    }
 
315
    return l;
 
316
}
 
317
 
 
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());
 
322
    }
 
323
}
 
324
 
 
325
}//namespace
 
 
b'\\ No newline at end of file'