46
|
1 #include "gui/widgets/sidebar.h"
|
|
2 #include <QFrame>
|
|
3 #include <QListWidget>
|
|
4 #include <QListWidgetItem>
|
|
5 #include <QMessageBox>
|
|
6 #include <QMouseEvent>
|
|
7
|
|
8 SideBar::SideBar(QWidget* parent) : QListWidget(parent) {
|
|
9 setFrameShape(QFrame::NoFrame);
|
|
10 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
11 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
12 setSelectionMode(QAbstractItemView::SingleSelection);
|
|
13 setSelectionBehavior(QAbstractItemView::SelectItems);
|
|
14 setMouseTracking(true);
|
|
15 /* FIXME: is there an easy way to do this with palettes? */
|
|
16 setStyleSheet("QListWidget::item:disabled { background: transparent }");
|
|
17 viewport()->setAutoFillBackground(false);
|
|
18
|
|
19 QFont font;
|
|
20 font.setPointSize(9);
|
|
21 setFont(font);
|
|
22
|
|
23 connect(this, &QListWidget::currentRowChanged, this,
|
|
24 [this](int index) { emit CurrentItemChanged(RemoveSeparatorsFromIndex(index)); });
|
|
25 }
|
|
26
|
|
27 QListWidgetItem* SideBar::AddItem(QString name, QIcon icon) {
|
|
28 QListWidgetItem* item = new QListWidgetItem(this);
|
|
29 item->setText(name);
|
|
30 if (!icon.isNull())
|
|
31 item->setIcon(icon);
|
|
32 return item;
|
|
33 }
|
|
34
|
|
35 QIcon SideBar::CreateIcon(const char* file) {
|
|
36 QPixmap pixmap(file, "PNG");
|
|
37 QIcon result;
|
|
38 result.addPixmap(pixmap, QIcon::Normal);
|
|
39 result.addPixmap(pixmap, QIcon::Selected);
|
|
40 return result;
|
|
41 }
|
|
42
|
|
43 QListWidgetItem* SideBar::AddSeparator() {
|
|
44 QListWidgetItem* item = new QListWidgetItem(this);
|
|
45 QFrame* line = new QFrame(this);
|
|
46 line->setFrameShape(QFrame::HLine);
|
|
47 line->setFrameShadow(QFrame::Sunken);
|
|
48 line->setMouseTracking(true);
|
|
49 line->setEnabled(false);
|
|
50
|
|
51 /*
|
|
52 QPalette pal;
|
|
53 pal.setColor(QPalette::Window, Qt::transparent);
|
|
54 line->setPalette(pal);
|
|
55 */
|
|
56
|
|
57 setItemWidget(item, line);
|
|
58 item->setFlags(Qt::NoItemFlags);
|
|
59 return item;
|
|
60 }
|
|
61
|
|
62 int SideBar::RemoveSeparatorsFromIndex(int index) {
|
|
63 int i, j;
|
|
64 for (i = 0, j = 0; i < index; i++) {
|
|
65 if (!IndexIsSeparator(indexFromItem(item(i))))
|
|
66 j++;
|
|
67 }
|
|
68 return j;
|
|
69 }
|
|
70
|
|
71 bool SideBar::IndexIsSeparator(QModelIndex index) const {
|
|
72 return !(index.isValid() && index.flags() & Qt::ItemIsEnabled);
|
|
73 }
|
|
74
|
|
75 QItemSelectionModel::SelectionFlags SideBar::selectionCommand(const QModelIndex& index, const QEvent*) const {
|
|
76 if (IndexIsSeparator(index))
|
|
77 return QItemSelectionModel::NoUpdate;
|
|
78 return QItemSelectionModel::ClearAndSelect;
|
|
79 }
|
|
80
|
|
81 void SideBar::mouseMoveEvent(QMouseEvent* event) {
|
|
82 if (!IndexIsSeparator(indexAt(event->pos())))
|
|
83 setCursor(Qt::PointingHandCursor);
|
|
84 else
|
|
85 unsetCursor();
|
|
86 QListView::mouseMoveEvent(event);
|
|
87 }
|
|
88
|
|
89 #include "gui/widgets/moc_sidebar.cpp"
|