~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to app/sidebar.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
// Local
42
42
 
43
 
namespace Gwenview {
44
 
 
 
43
namespace Gwenview
 
44
{
45
45
 
46
46
/**
47
47
 * A button which always leave room for an icon, even if there is none, so that
48
48
 * all button texts are correctly aligned.
49
49
 */
50
 
class SideBarButton : public QToolButton {
 
50
class SideBarButton : public QToolButton
 
51
{
51
52
protected:
52
 
        virtual void paintEvent(QPaintEvent* event) {
53
 
                forceIcon();
54
 
                QToolButton::paintEvent(event);
55
 
        }
 
53
    virtual void paintEvent(QPaintEvent* event)
 
54
    {
 
55
        forceIcon();
 
56
        QToolButton::paintEvent(event);
 
57
    }
56
58
 
57
 
        virtual QSize sizeHint() const {
58
 
                const_cast<SideBarButton*>(this)->forceIcon();
59
 
                return QToolButton::sizeHint();
60
 
        }
 
59
    virtual QSize sizeHint() const {
 
60
        const_cast<SideBarButton*>(this)->forceIcon();
 
61
        return QToolButton::sizeHint();
 
62
    }
61
63
 
62
64
private:
63
 
        void forceIcon() {
64
 
                if (!icon().isNull()) {
65
 
                        return;
66
 
                }
67
 
 
68
 
                // Assign an empty icon to the button if there is no icon associated
69
 
                // with the action so that all button texts are correctly aligned.
70
 
                QSize wantedSize = iconSize();
71
 
                if (mEmptyIcon.isNull() || mEmptyIcon.actualSize(wantedSize) != wantedSize) {
72
 
                        QPixmap pix(wantedSize);
73
 
                        pix.fill(Qt::transparent);
74
 
                        mEmptyIcon.addPixmap(pix);
75
 
                }
76
 
                setIcon(mEmptyIcon);
77
 
        }
78
 
 
79
 
        QIcon mEmptyIcon;
 
65
    void forceIcon()
 
66
    {
 
67
        if (!icon().isNull()) {
 
68
            return;
 
69
        }
 
70
 
 
71
        // Assign an empty icon to the button if there is no icon associated
 
72
        // with the action so that all button texts are correctly aligned.
 
73
        QSize wantedSize = iconSize();
 
74
        if (mEmptyIcon.isNull() || mEmptyIcon.actualSize(wantedSize) != wantedSize) {
 
75
            QPixmap pix(wantedSize);
 
76
            pix.fill(Qt::transparent);
 
77
            mEmptyIcon.addPixmap(pix);
 
78
        }
 
79
        setIcon(mEmptyIcon);
 
80
    }
 
81
 
 
82
    QIcon mEmptyIcon;
80
83
};
81
84
 
82
 
 
83
85
//- SideBarGroup ---------------------------------------------------------------
84
86
struct SideBarGroupPrivate {
85
 
        QFrame* mContainer;
86
 
        QLabel* mTitleLabel;
 
87
    QFrame* mContainer;
 
88
    QLabel* mTitleLabel;
87
89
};
88
90
 
89
 
 
90
91
SideBarGroup::SideBarGroup(const QString& title)
91
92
: QFrame()
92
 
, d(new SideBarGroupPrivate) {
93
 
        d->mContainer = 0;
94
 
        d->mTitleLabel = new QLabel(this);
95
 
        d->mTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
96
 
        d->mTitleLabel->setFixedHeight(d->mTitleLabel->sizeHint().height() * 3 / 2);
97
 
        QFont font(d->mTitleLabel->font());
98
 
        font.setBold(true);
99
 
        d->mTitleLabel->setFont(font);
100
 
        d->mTitleLabel->setText(title);
101
 
 
102
 
        QVBoxLayout* layout = new QVBoxLayout(this);
103
 
        layout->setMargin(0);
104
 
        layout->setSpacing(0);
105
 
 
106
 
        layout->addWidget(d->mTitleLabel);
107
 
 
108
 
        clear();
109
 
}
110
 
 
111
 
 
112
 
SideBarGroup::~SideBarGroup() {
113
 
        delete d;
114
 
}
115
 
 
116
 
 
117
 
void SideBarGroup::paintEvent(QPaintEvent* event) {
118
 
        QFrame::paintEvent(event);
119
 
        if (parentWidget()->layout()->indexOf(this) != 0) {
120
 
                // Draw a separator, but only if we are not the first group
121
 
                QPainter painter(this);
122
 
                QPen pen(palette().mid().color());
123
 
                painter.setPen(pen);
124
 
                painter.drawLine(rect().topLeft(), rect().topRight());
125
 
        }
126
 
}
127
 
 
128
 
 
129
 
void SideBarGroup::addWidget(QWidget* widget) {
130
 
        widget->setParent(d->mContainer);
131
 
        d->mContainer->layout()->addWidget(widget);
132
 
}
133
 
 
134
 
 
135
 
void SideBarGroup::clear() {
136
 
        if (d->mContainer) {
137
 
                d->mContainer->deleteLater();
138
 
        }
139
 
 
140
 
        d->mContainer = new QFrame(this);
141
 
        QVBoxLayout* containerLayout = new QVBoxLayout(d->mContainer);
142
 
        containerLayout->setMargin(0);
143
 
        containerLayout->setSpacing(0);
144
 
 
145
 
        layout()->addWidget(d->mContainer);
146
 
}
147
 
 
148
 
 
149
 
void SideBarGroup::addAction(QAction* action) {
150
 
        int size = KIconLoader::global()->currentSize(KIconLoader::Small);
151
 
        QToolButton* button = new SideBarButton();
152
 
        button->setFocusPolicy(Qt::NoFocus);
153
 
        button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
154
 
        button->setAutoRaise(true);
155
 
        button->setDefaultAction(action);
156
 
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
157
 
        button->setIconSize(QSize(size, size));
158
 
        if (action->menu()) {
159
 
                button->setPopupMode(QToolButton::InstantPopup);
160
 
        }
161
 
        addWidget(button);
162
 
}
163
 
 
 
93
, d(new SideBarGroupPrivate)
 
94
{
 
95
    d->mContainer = 0;
 
96
    d->mTitleLabel = new QLabel(this);
 
97
    d->mTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
 
98
    d->mTitleLabel->setFixedHeight(d->mTitleLabel->sizeHint().height() * 3 / 2);
 
99
    QFont font(d->mTitleLabel->font());
 
100
    font.setBold(true);
 
101
    d->mTitleLabel->setFont(font);
 
102
    d->mTitleLabel->setText(title);
 
103
 
 
104
    QVBoxLayout* layout = new QVBoxLayout(this);
 
105
    layout->setMargin(0);
 
106
    layout->setSpacing(0);
 
107
 
 
108
    layout->addWidget(d->mTitleLabel);
 
109
 
 
110
    clear();
 
111
}
 
112
 
 
113
SideBarGroup::~SideBarGroup()
 
114
{
 
115
    delete d;
 
116
}
 
117
 
 
118
void SideBarGroup::paintEvent(QPaintEvent* event)
 
119
{
 
120
    QFrame::paintEvent(event);
 
121
    if (parentWidget()->layout()->indexOf(this) != 0) {
 
122
        // Draw a separator, but only if we are not the first group
 
123
        QPainter painter(this);
 
124
        QPen pen(palette().mid().color());
 
125
        painter.setPen(pen);
 
126
        painter.drawLine(rect().topLeft(), rect().topRight());
 
127
    }
 
128
}
 
129
 
 
130
void SideBarGroup::addWidget(QWidget* widget)
 
131
{
 
132
    widget->setParent(d->mContainer);
 
133
    d->mContainer->layout()->addWidget(widget);
 
134
}
 
135
 
 
136
void SideBarGroup::clear()
 
137
{
 
138
    if (d->mContainer) {
 
139
        d->mContainer->deleteLater();
 
140
    }
 
141
 
 
142
    d->mContainer = new QFrame(this);
 
143
    QVBoxLayout* containerLayout = new QVBoxLayout(d->mContainer);
 
144
    containerLayout->setMargin(0);
 
145
    containerLayout->setSpacing(0);
 
146
 
 
147
    layout()->addWidget(d->mContainer);
 
148
}
 
149
 
 
150
void SideBarGroup::addAction(QAction* action)
 
151
{
 
152
    int size = KIconLoader::global()->currentSize(KIconLoader::Small);
 
153
    QToolButton* button = new SideBarButton();
 
154
    button->setFocusPolicy(Qt::NoFocus);
 
155
    button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
 
156
    button->setAutoRaise(true);
 
157
    button->setDefaultAction(action);
 
158
    button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
159
    button->setIconSize(QSize(size, size));
 
160
    if (action->menu()) {
 
161
        button->setPopupMode(QToolButton::InstantPopup);
 
162
    }
 
163
    addWidget(button);
 
164
}
164
165
 
165
166
//- SideBarPage ----------------------------------------------------------------
166
167
struct SideBarPagePrivate {
167
 
        QString mTitle;
168
 
        QVBoxLayout* mLayout;
 
168
    QString mTitle;
 
169
    QVBoxLayout* mLayout;
169
170
};
170
171
 
171
 
 
172
172
SideBarPage::SideBarPage(const QString& title)
173
173
: QWidget()
174
174
, d(new SideBarPagePrivate)
175
175
{
176
 
        d->mTitle = title;
177
 
 
178
 
        d->mLayout = new QVBoxLayout(this);
179
 
        d->mLayout->setMargin(0);
180
 
}
181
 
 
182
 
 
183
 
SideBarPage::~SideBarPage() {
184
 
        delete d;
185
 
}
186
 
 
187
 
 
188
 
const QString& SideBarPage::title() const {
189
 
        return d->mTitle;
190
 
}
191
 
 
192
 
 
193
 
void SideBarPage::addWidget(QWidget* widget) {
194
 
        d->mLayout->addWidget(widget);
195
 
}
196
 
 
197
 
 
198
 
void SideBarPage::addStretch() {
199
 
        d->mLayout->addStretch();
200
 
}
201
 
 
 
176
    d->mTitle = title;
 
177
 
 
178
    d->mLayout = new QVBoxLayout(this);
 
179
    d->mLayout->setMargin(0);
 
180
}
 
181
 
 
182
SideBarPage::~SideBarPage()
 
183
{
 
184
    delete d;
 
185
}
 
186
 
 
187
const QString& SideBarPage::title() const
 
188
{
 
189
    return d->mTitle;
 
190
}
 
191
 
 
192
void SideBarPage::addWidget(QWidget* widget)
 
193
{
 
194
    d->mLayout->addWidget(widget);
 
195
}
 
196
 
 
197
void SideBarPage::addStretch()
 
198
{
 
199
    d->mLayout->addStretch();
 
200
}
202
201
 
203
202
//- SideBar --------------------------------------------------------------------
204
203
struct SideBarPrivate {
205
204
};
206
205
 
207
 
 
208
206
SideBar::SideBar(QWidget* parent)
209
207
: QTabWidget(parent)
210
 
, d(new SideBarPrivate) {
211
 
        setFont(KGlobalSettings::smallestReadableFont());
212
 
        tabBar()->setDocumentMode(true);
213
 
        tabBar()->setUsesScrollButtons(false);
214
 
        tabBar()->setFocusPolicy(Qt::NoFocus);
215
 
        setTabPosition(QTabWidget::South);
216
 
        setElideMode(Qt::ElideRight);
217
 
}
218
 
 
219
 
 
220
 
SideBar::~SideBar() {
221
 
        delete d;
222
 
}
223
 
 
224
 
 
225
 
QSize SideBar::sizeHint() const {
226
 
        return QSize(200, 200);
227
 
}
228
 
 
229
 
 
230
 
void SideBar::addPage(SideBarPage* page) {
231
 
        addTab(page, page->title());
232
 
}
233
 
 
234
 
 
235
 
QString SideBar::currentPage() const {
236
 
        return currentWidget()->objectName();
237
 
}
238
 
 
239
 
 
240
 
void SideBar::setCurrentPage(const QString& name) {
241
 
        for (int index = 0; index < count(); ++index) {
242
 
                if (widget(index)->objectName() == name) {
243
 
                        setCurrentIndex(index);
244
 
                }
245
 
        }
246
 
}
247
 
 
 
208
, d(new SideBarPrivate)
 
209
{
 
210
    setFont(KGlobalSettings::smallestReadableFont());
 
211
    tabBar()->setDocumentMode(true);
 
212
    tabBar()->setUsesScrollButtons(false);
 
213
    tabBar()->setFocusPolicy(Qt::NoFocus);
 
214
    setTabPosition(QTabWidget::South);
 
215
    setElideMode(Qt::ElideRight);
 
216
}
 
217
 
 
218
SideBar::~SideBar()
 
219
{
 
220
    delete d;
 
221
}
 
222
 
 
223
QSize SideBar::sizeHint() const
 
224
{
 
225
    return QSize(200, 200);
 
226
}
 
227
 
 
228
void SideBar::addPage(SideBarPage* page)
 
229
{
 
230
    addTab(page, page->title());
 
231
}
 
232
 
 
233
QString SideBar::currentPage() const
 
234
{
 
235
    return currentWidget()->objectName();
 
236
}
 
237
 
 
238
void SideBar::setCurrentPage(const QString& name)
 
239
{
 
240
    for (int index = 0; index < count(); ++index) {
 
241
        if (widget(index)->objectName() == name) {
 
242
            setCurrentIndex(index);
 
243
        }
 
244
    }
 
245
}
248
246
 
249
247
} // namespace