| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "view.h" |
| 22 |
|
| 23 |
#include <QListWidget> |
| 24 |
#include <QHBoxLayout> |
| 25 |
#include <QStackedWidget> |
| 26 |
#include <QLabel> |
| 27 |
#include <QPainter> |
| 28 |
#include <QtDebug> |
| 29 |
|
| 30 |
namespace SIF { |
| 31 |
|
| 32 |
class List : public QListWidget |
| 33 |
{ |
| 34 |
Q_OBJECT; |
| 35 |
public: |
| 36 |
List(View *view, QStackedWidget *stack); |
| 37 |
~List(); |
| 38 |
|
| 39 |
void addWidget(const QString &title, const QIcon &icon, QWidget *w); |
| 40 |
|
| 41 |
private slots: |
| 42 |
void onCurrentItemChanged(QListWidgetItem *item, QListWidgetItem *); |
| 43 |
|
| 44 |
protected: |
| 45 |
void resizeEvent(QResizeEvent *e); |
| 46 |
void rowsInserted(const QModelIndex &parent, int start, int end); |
| 47 |
|
| 48 |
private: |
| 49 |
View *m_view; |
| 50 |
QStackedWidget *m_stack; |
| 51 |
QHash<QListWidgetItem *, QWidget *> m_widgets; |
| 52 |
}; |
| 53 |
|
| 54 |
List::List(View *view, QStackedWidget *stack) : m_view(view), m_stack(stack) |
| 55 |
{ |
| 56 |
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 57 |
setMaximumWidth(128); |
| 58 |
setIconSize(QSize(48, 48)); |
| 59 |
setAlternatingRowColors(true); |
| 60 |
|
| 61 |
connect(this, SIGNAL(currentItemChanged( QListWidgetItem*,QListWidgetItem* )), this, SLOT(onCurrentItemChanged(QListWidgetItem *, QListWidgetItem*))); |
| 62 |
} |
| 63 |
|
| 64 |
List::~List() |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
void List::addWidget(const QString &title, const QIcon &icon, QWidget *w) |
| 69 |
{ |
| 70 |
QListWidgetItem *item = new QListWidgetItem(title); |
| 71 |
|
| 72 |
if(!icon.isNull()) |
| 73 |
{ |
| 74 |
item->setIcon(icon); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
QPixmap px(48, 48); |
| 79 |
px.fill(Qt::transparent); |
| 80 |
QPainter painter(&px); |
| 81 |
painter.setRenderHints(QPainter::Antialiasing); |
| 82 |
|
| 83 |
painter.setPen(Qt::NoPen); |
| 84 |
|
| 85 |
QRadialGradient brush(px.rect().center(), px.rect().width()/2 - 10); |
| 86 |
brush.setColorAt(0.0, Qt::gray); |
| 87 |
brush.setColorAt(1.0, Qt::black); |
| 88 |
|
| 89 |
painter.setBrush(brush); |
| 90 |
painter.drawEllipse(px.rect().adjusted(10, 10, -10, -10) ); |
| 91 |
|
| 92 |
painter.end(); |
| 93 |
|
| 94 |
item->setIcon(px); |
| 95 |
} |
| 96 |
|
| 97 |
this->addItem(item); |
| 98 |
|
| 99 |
m_stack->addWidget(w); |
| 100 |
|
| 101 |
m_widgets.insert(item, w); |
| 102 |
} |
| 103 |
|
| 104 |
void List::onCurrentItemChanged(QListWidgetItem *item, QListWidgetItem *) |
| 105 |
{ |
| 106 |
if(item) |
| 107 |
{ |
| 108 |
if(m_widgets.contains(item)) |
| 109 |
{ |
| 110 |
m_stack->setCurrentWidget(m_widgets[item]); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void List::resizeEvent(QResizeEvent *e) |
| 116 |
{ |
| 117 |
QListWidget::resizeEvent(e); |
| 118 |
} |
| 119 |
|
| 120 |
void List::rowsInserted(const QModelIndex &parent, int start, int end) |
| 121 |
{ |
| 122 |
int size = 0; |
| 123 |
|
| 124 |
|
| 125 |
for(int i = start; i <= end; i++) |
| 126 |
{ |
| 127 |
QListWidgetItem *it = item(i); |
| 128 |
|
| 129 |
if(it) |
| 130 |
{ |
| 131 |
QFontMetrics fm(it->font()); |
| 132 |
|
| 133 |
int tmp = fm.width(it->text()) + iconSize().width() + 30; |
| 134 |
|
| 135 |
if(tmp > size) |
| 136 |
{ |
| 137 |
size = tmp; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if(size > maximumWidth()) |
| 143 |
{ |
| 144 |
setMaximumWidth(size); |
| 145 |
} |
| 146 |
|
| 147 |
QListWidget::rowsInserted(parent, start, end); |
| 148 |
} |
| 149 |
|
| 150 |
struct View::Private { |
| 151 |
List *list; |
| 152 |
QStackedWidget *stack; |
| 153 |
}; |
| 154 |
|
| 155 |
View::View(QWidget *parent) |
| 156 |
: QWidget(parent), d(new Private) |
| 157 |
{ |
| 158 |
QHBoxLayout *layout = new QHBoxLayout(this); |
| 159 |
layout->setMargin(3); |
| 160 |
|
| 161 |
d->stack = new QStackedWidget; |
| 162 |
d->list = new List(this, d->stack); |
| 163 |
|
| 164 |
d->list->setResizeMode(QListView::Fixed); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
layout->addWidget(d->list); |
| 169 |
layout->addWidget(d->stack); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
View::~View() |
| 174 |
{ |
| 175 |
delete d; |
| 176 |
} |
| 177 |
|
| 178 |
void View::addPage(const QString &title, QWidget *w) |
| 179 |
{ |
| 180 |
d->list->addWidget(title, QIcon(), w); |
| 181 |
} |
| 182 |
|
| 183 |
void View::addPage(const QIcon &icon, const QString &title, QWidget *w) |
| 184 |
{ |
| 185 |
d->list->addWidget(title, icon, w); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
#include "view.moc" |