1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
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 "AnnotHighlightTree.h"
23 
24 #include <QColorDialog>
25 #include <QHeaderView>
26 
27 #include <U2Core/AnnotationSettings.h>
28 #include <U2Core/U2SafePoints.h>
29 
30 #include "AnnotHighlightTreeItem.h"
31 
32 namespace U2 {
33 
34 const int AnnotHighlightTree::COL_NUM_ANNOT_NAME = 0;
35 const int AnnotHighlightTree::COL_NUM_COLOR = 1;
36 
37 const int AnnotHighlightTree::COLOR_COLUMN_WIDTH = 60;
38 const int AnnotHighlightTree::INITIAL_TREE_HEIGHT = 25;
39 
AnnotHighlightTree()40 AnnotHighlightTree::AnnotHighlightTree() {
41     setObjectName("OP_ANNOT_HIGHLIGHT_TREE");
42 
43     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
44     setIndentation(0);
45 
46     setSelectionMode(SingleSelection);
47 
48     annotTreeHeight = INITIAL_TREE_HEIGHT;
49 
50     QStringList headerLabels;
51     headerLabels << QObject::tr("Annotation") << QObject::tr("Color");
52     setHeaderLabels(headerLabels);
53 
54     header()->setSectionResizeMode(COL_NUM_ANNOT_NAME, QHeaderView::Stretch);
55     header()->setSectionResizeMode(COL_NUM_COLOR, QHeaderView::Fixed);
56     header()->setStretchLastSection(false);
57     header()->resizeSection(COL_NUM_COLOR, COLOR_COLUMN_WIDTH);
58 
59     setStyleSheet("QTreeWidget#OP_ANNOT_HIGHLIGHT_TREE { "
60                   "border-style: solid;"
61                   "border-color: palette(mid);"
62                   "border-width: 1px;"
63                   "background: white;"
64                   "margin-left: 5px;"
65                   "margin-right: 10px;"
66                   " }");
67 
68     // Emit signal that annotation type has changed
69     connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), SLOT(sl_onCurrentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
70 
71     // Listen for click on "Color" column
72     connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)), SLOT(sl_onItemClicked(QTreeWidgetItem *, int)));
73 }
74 
addItem(QString annotName,QColor annotColor)75 void AnnotHighlightTree::addItem(QString annotName, QColor annotColor) {
76     // Create and initialize the tree item
77     QTreeWidgetItem *item = new AnnotHighlightTreeItem(annotName, annotColor);
78 
79     // Add the item and increase the tree height
80     annotTreeHeight += AnnotHighlightTreeItem::ROW_HEIGHT;
81     QTreeWidget::addTopLevelItem(item);
82     updateGeometry();
83 }
84 
clear()85 void AnnotHighlightTree::clear() {
86     annotTreeHeight = INITIAL_TREE_HEIGHT;
87     QTreeWidget::clear();
88 }
89 
setFirstItemSelected()90 void AnnotHighlightTree::setFirstItemSelected() {
91     QTreeWidgetItem *firstItem = topLevelItem(0);
92     SAFE_POINT(0 != firstItem, "There is no first item in the tree!", );
93     setCurrentItem(firstItem);
94 }
95 
getFirstItemAnnotName()96 QString AnnotHighlightTree::getFirstItemAnnotName() {
97     QTreeWidgetItem *firstItem = topLevelItem(0);
98     SAFE_POINT(0 != firstItem, "There is no first item in the tree!", QString());
99 
100     QString annotName = firstItem->text(COL_NUM_ANNOT_NAME);
101     return annotName;
102 }
103 
getCurrentItemAnnotName()104 QString AnnotHighlightTree::getCurrentItemAnnotName() {
105     QTreeWidgetItem *current = currentItem();
106     if (0 != current) {
107         return current->text(COL_NUM_ANNOT_NAME);
108     } else {
109         return QString();
110     }
111 }
112 
setItemSelectedWithAnnotName(QString annotName)113 void AnnotHighlightTree::setItemSelectedWithAnnotName(QString annotName) {
114     CHECK(!annotName.isEmpty(), );
115     QList<QTreeWidgetItem *> items = findItems(annotName, Qt::MatchExactly, COL_NUM_ANNOT_NAME);
116     SAFE_POINT(1 == items.count(), "Exactly one tree item with the specified annotation name should have been found.", );
117 
118     setCurrentItem(items[0]);
119 }
120 
sl_onCurrentItemChanged(QTreeWidgetItem * current,QTreeWidgetItem *)121 void AnnotHighlightTree::sl_onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem * /* previous */) {
122     if (0 != current) {
123         QString annotName = current->text(COL_NUM_ANNOT_NAME);
124         emit si_selectedItemChanged(annotName);
125     }
126 }
127 
sl_onItemClicked(QTreeWidgetItem * item,int column)128 void AnnotHighlightTree::sl_onItemClicked(QTreeWidgetItem *item, int column) {
129     if (column != COL_NUM_COLOR) {
130         return;
131     }
132 
133     AnnotHighlightTreeItem *annotHighlightItem = static_cast<AnnotHighlightTreeItem *>(item);
134 
135     QColorDialog::ColorDialogOption options = static_cast<QColorDialog::ColorDialogOption>(0);
136     if (qgetenv(ENV_GUI_TEST) == "1") {
137         options = QColorDialog::DontUseNativeDialog;
138     }
139     QColor annotColor = QColorDialog::getColor(annotHighlightItem->getColor(), this, "", options);
140     if (annotColor.isValid()) {
141         annotHighlightItem->setColor(annotColor);
142         emit si_colorChanged(annotHighlightItem->getName(), annotColor);
143     }
144 }
145 
146 }  // namespace U2
147