~ubuntu-branches/ubuntu/quantal/gwenview/quantal-proposed

1.1.5 by Jonathan Riddell
Import upstream version 4.7.90
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
1 by Rohan Garg
Import upstream version 4.6.90+repack
2
/*
3
Gwenview: an image viewer
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
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, MA 02110-1301, USA.
19
20
*/
21
// Self
22
#include "savebar.moc"
23
24
// Qt
25
#include <QHBoxLayout>
26
#include <QLabel>
27
#include <QToolButton>
28
#include <QToolTip>
29
30
// KDE
1.1.6 by Philip Muškovac
Import upstream version 4.7.95
31
#include <KActionCollection>
32
#include <KColorScheme>
33
#include <KDebug>
34
#include <KIcon>
35
#include <KIconLoader>
36
#include <KLocale>
37
#include <KUrl>
1 by Rohan Garg
Import upstream version 4.6.90+repack
38
39
// Local
40
#include "lib/document/documentfactory.h"
41
#include "lib/gwenviewconfig.h"
42
#include "lib/memoryutils.h"
43
#include "lib/paintutils.h"
44
1.1.5 by Jonathan Riddell
Import upstream version 4.7.90
45
namespace Gwenview
46
{
47
48
QToolButton* createToolButton()
49
{
50
    QToolButton* button = new QToolButton;
51
    button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
52
    button->hide();
53
    return button;
1 by Rohan Garg
Import upstream version 4.6.90+repack
54
}
55
1.1.6 by Philip Muškovac
Import upstream version 4.7.95
56
struct SaveBarPrivate
57
{
1.1.5 by Jonathan Riddell
Import upstream version 4.7.90
58
    SaveBar* q;
59
    KActionCollection* mActionCollection;
60
    QWidget* mSaveBarWidget;
61
    QWidget* mTopRowWidget;
62
    QToolButton* mUndoButton;
63
    QToolButton* mRedoButton;
64
    QToolButton* mSaveCurrentUrlButton;
65
    QToolButton* mSaveAsButton;
66
    QToolButton* mSaveAllButton;
67
    QToolButton* mSaveAllFullScreenButton;
68
    QLabel* mMessageLabel;
69
    QLabel* mActionsLabel;
70
    QFrame* mTooManyChangesFrame;
71
    KUrl mCurrentUrl;
72
    bool mFullScreenMode;
73
74
    void createTooManyChangesFrame()
75
    {
76
        mTooManyChangesFrame = new QFrame;
77
78
        // Icon
79
        QLabel* iconLabel = new QLabel;
80
        QPixmap pix = KIconLoader::global()->loadIcon(
81
                          "dialog-warning", KIconLoader::Dialog, KIconLoader::SizeSmall);
82
        iconLabel->setPixmap(pix);
83
84
        // Text label
85
        QLabel* textLabel = new QLabel;
86
        textLabel->setText(
87
            i18n("You have modified many images. To avoid memory problems, you should save your changes.")
88
        );
89
90
        mSaveAllFullScreenButton = createToolButton();
91
92
        // Layout
93
        QHBoxLayout* layout = new QHBoxLayout(mTooManyChangesFrame);
94
        layout->setMargin(0);
95
        layout->addWidget(iconLabel);
96
        layout->addWidget(textLabel);
97
        layout->addWidget(mSaveAllFullScreenButton);
98
        mTooManyChangesFrame->hide();
99
100
        // CSS
101
        KColorScheme scheme(mSaveBarWidget->palette().currentColorGroup(), KColorScheme::Window);
102
        QColor warningBackgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
103
        QColor warningBorderColor = PaintUtils::adjustedHsv(warningBackgroundColor, 0, 150, 0);
104
        QColor warningColor = scheme.foreground(KColorScheme::NegativeText).color();
105
106
        QString css =
107
            ".QFrame {"
108
            "	background-color: %1;"
109
            "	border: 1px solid %2;"
110
            "	border-radius: 4px;"
111
            "	padding: 3px;"
112
            "}"
113
            ".QFrame QLabel {"
114
            "	color: %3;"
115
            "}"
116
            ;
117
        css = css
118
              .arg(warningBackgroundColor.name())
119
              .arg(warningBorderColor.name())
120
              .arg(warningColor.name())
121
              ;
122
        mTooManyChangesFrame->setStyleSheet(css);
123
    }
124
125
    void applyNormalStyleSheet()
126
    {
127
        QColor bgColor = QToolTip::palette().base().color();
128
        QColor borderColor = PaintUtils::adjustedHsv(bgColor, 0, 150, 0);
129
        QColor fgColor = QToolTip::palette().text().color();
130
131
        QString css =
132
            "#saveBarWidget {"
133
            "	background-color: %1;"
134
            "	border-top: 1px solid %2;"
135
            "	border-bottom: 1px solid %2;"
136
            "	color: %3;"
137
            "}"
138
            ;
139
140
        css = css
141
              .arg(bgColor.name())
142
              .arg(borderColor.name())
143
              .arg(fgColor.name())
144
              ;
145
        mSaveBarWidget->setStyleSheet(css);
146
    }
147
148
    void applyFullScreenStyleSheet()
149
    {
150
        QString css =
151
            "#saveBarWidget {"
152
            "	background-color: #333;"
153
            "}";
154
        mSaveBarWidget->setStyleSheet(css);
155
    }
156
157
    void updateTooManyChangesFrame(const QList<KUrl>& list)
158
    {
159
        qreal maxPercentageOfMemoryUsage = GwenviewConfig::percentageOfMemoryUsageWarning();
160
        qulonglong maxMemoryUsage = MemoryUtils::getTotalMemory() * maxPercentageOfMemoryUsage;
161
        qulonglong memoryUsage = 0;
162
        Q_FOREACH(const KUrl & url, list) {
163
            Document::Ptr doc = DocumentFactory::instance()->load(url);
164
            memoryUsage += doc->memoryUsage();
165
        }
166
167
        mTooManyChangesFrame->setVisible(memoryUsage > maxMemoryUsage);
168
    }
169
170
    void updateTopRowWidget(const QList<KUrl>& lst)
171
    {
172
        QStringList links;
173
        QString message;
174
175
        if (lst.contains(mCurrentUrl)) {
176
            message = i18n("Current image modified");
177
178
            mUndoButton->show();
179
            mRedoButton->show();
180
181
            if (lst.size() > 1) {
182
                QString previous = i18n("Previous modified image");
183
                QString next = i18n("Next modified image");
184
                if (mCurrentUrl == lst[0]) {
185
                    links << previous;
186
                } else {
187
                    links << QString("<a href='previous'>%1</a>").arg(previous);
188
                }
189
                if (mCurrentUrl == lst[lst.size() - 1]) {
190
                    links << next;
191
                } else {
192
                    links << QString("<a href='next'>%1</a>").arg(next);
193
                }
194
            }
195
        } else {
196
            mUndoButton->hide();
197
            mRedoButton->hide();
198
199
            message = i18np("One image modified", "%1 images modified", lst.size());
200
            if (lst.size() > 1) {
201
                links << QString("<a href='first'>%1</a>").arg(i18n("Go to first modified image"));
202
            } else {
203
                links << QString("<a href='first'>%1</a>").arg(i18n("Go to it"));
204
            }
205
        }
206
207
        mSaveCurrentUrlButton->setVisible(lst.contains(mCurrentUrl));
208
        mSaveAsButton->setVisible(lst.contains(mCurrentUrl));
209
        mSaveAllButton->setVisible(lst.size() >= 1);
210
211
        mMessageLabel->setText(message);
212
        mActionsLabel->setText(links.join(" | "));
213
    }
214
215
    void updateWidgetSizes()
216
    {
217
        QVBoxLayout* layout = static_cast<QVBoxLayout*>(mSaveBarWidget->layout());
218
        int topRowHeight = mFullScreenMode ? 0 : mTopRowWidget->height();
219
        int bottomRowHeight = mTooManyChangesFrame->isVisibleTo(mSaveBarWidget) ? mTooManyChangesFrame->sizeHint().height() : 0;
220
221
        int height = 2 * layout->margin() + topRowHeight + bottomRowHeight;
222
        if (topRowHeight > 0 && bottomRowHeight > 0) {
223
            height += layout->spacing();
224
        }
225
        mSaveBarWidget->setFixedHeight(height);
226
    }
1 by Rohan Garg
Import upstream version 4.6.90+repack
227
};
228
229
SaveBar::SaveBar(QWidget* parent, KActionCollection* actionCollection)
230
: SlideContainer(parent)
1.1.5 by Jonathan Riddell
Import upstream version 4.7.90
231
, d(new SaveBarPrivate)
232
{
233
    d->q = this;
234
    d->mFullScreenMode = false;
235
    d->mActionCollection = actionCollection;
236
    d->mSaveBarWidget = new QWidget();
237
    d->mSaveBarWidget->setObjectName(QLatin1String("saveBarWidget"));
238
    d->applyNormalStyleSheet();
239
240
    d->mMessageLabel = new QLabel;
241
    d->mMessageLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
242
243
    d->mUndoButton = createToolButton();
244
    d->mRedoButton = createToolButton();
245
    d->mSaveCurrentUrlButton = createToolButton();
246
    d->mSaveAsButton = createToolButton();
247
    d->mSaveAllButton = createToolButton();
248
249
    d->mActionsLabel = new QLabel;
250
    d->mActionsLabel->setAlignment(Qt::AlignCenter);
251
    d->mActionsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
252
253
    d->createTooManyChangesFrame();
254
255
    // Setup top row
256
    d->mTopRowWidget = new QWidget;
257
    QHBoxLayout* rowLayout = new QHBoxLayout(d->mTopRowWidget);
258
    rowLayout->addWidget(d->mMessageLabel);
259
    rowLayout->addWidget(d->mUndoButton);
260
    rowLayout->addWidget(d->mRedoButton);
261
    rowLayout->addWidget(d->mActionsLabel);
262
    rowLayout->addWidget(d->mSaveCurrentUrlButton);
263
    rowLayout->addWidget(d->mSaveAsButton);
264
    rowLayout->addWidget(d->mSaveAllButton);
265
    rowLayout->setMargin(0);
266
267
    // Setup bottom row
268
    QHBoxLayout* bottomRowLayout = new QHBoxLayout;
269
    bottomRowLayout->addStretch();
270
    bottomRowLayout->addWidget(d->mTooManyChangesFrame);
271
    bottomRowLayout->addStretch();
272
273
    // Gather everything together
274
    QVBoxLayout* layout = new QVBoxLayout(d->mSaveBarWidget);
275
    layout->addWidget(d->mTopRowWidget);
276
    layout->addLayout(bottomRowLayout);
277
    layout->setMargin(3);
278
    layout->setSpacing(3);
279
280
    setContent(d->mSaveBarWidget);
281
282
    connect(DocumentFactory::instance(), SIGNAL(modifiedDocumentListChanged()),
283
            SLOT(updateContent()));
284
285
    connect(d->mActionsLabel, SIGNAL(linkActivated(QString)),
286
            SLOT(triggerAction(QString)));
287
}
288
289
SaveBar::~SaveBar()
290
{
291
    delete d;
292
}
293
294
void SaveBar::initActionDependentWidgets()
295
{
296
    d->mUndoButton->setDefaultAction(d->mActionCollection->action("edit_undo"));
297
    d->mRedoButton->setDefaultAction(d->mActionCollection->action("edit_redo"));
298
    d->mSaveCurrentUrlButton->setDefaultAction(d->mActionCollection->action("file_save"));
299
    d->mSaveAsButton->setDefaultAction(d->mActionCollection->action("file_save_as"));
300
301
    // FIXME: Not using an action for now
302
    d->mSaveAllButton->setText(i18n("Save All"));
303
    d->mSaveAllButton->setIcon(KIcon("document-save-all"));
304
    connect(d->mSaveAllButton, SIGNAL(clicked()),
305
            SIGNAL(requestSaveAll()));
306
307
    d->mSaveAllFullScreenButton->setText(i18n("Save All"));
308
    connect(d->mSaveAllFullScreenButton, SIGNAL(clicked()),
309
            SIGNAL(requestSaveAll()));
310
311
    int height = d->mUndoButton->sizeHint().height();
312
    d->mTopRowWidget->setFixedHeight(height);
313
    d->updateWidgetSizes();
314
}
315
316
void SaveBar::setFullScreenMode(bool value)
317
{
318
    d->mFullScreenMode = value;
319
    d->mSaveAllFullScreenButton->setVisible(value);
320
    if (value) {
321
        d->applyFullScreenStyleSheet();
322
    } else {
323
        d->applyNormalStyleSheet();
324
    }
325
    updateContent();
326
}
327
328
void SaveBar::updateContent()
329
{
330
    QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
331
332
    if (d->mFullScreenMode) {
333
        d->mTopRowWidget->hide();
334
    } else {
335
        d->mTopRowWidget->show();
336
        d->updateTopRowWidget(lst);
337
    }
338
339
    d->updateTooManyChangesFrame(lst);
340
341
    d->updateWidgetSizes();
342
    if (lst.isEmpty() || (d->mFullScreenMode && !d->mTooManyChangesFrame->isVisibleTo(d->mSaveBarWidget))) {
343
        slideOut();
344
    } else {
345
        slideIn();
346
    }
347
}
348
349
void SaveBar::triggerAction(const QString& action)
350
{
351
    QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
352
    if (action == "first") {
353
        goToUrl(lst[0]);
354
    } else if (action == "previous") {
355
        int pos = lst.indexOf(d->mCurrentUrl);
356
        --pos;
357
        Q_ASSERT(pos >= 0);
358
        goToUrl(lst[pos]);
359
    } else if (action == "next") {
360
        int pos = lst.indexOf(d->mCurrentUrl);
361
        ++pos;
362
        Q_ASSERT(pos < lst.size());
363
        goToUrl(lst[pos]);
364
    } else {
365
        kWarning() << "Unknown action: " << action ;
366
    }
367
}
368
369
void SaveBar::setCurrentUrl(const KUrl& url)
370
{
371
    d->mCurrentUrl = url;
372
    updateContent();
373
}
1 by Rohan Garg
Import upstream version 4.6.90+repack
374
375
} // namespace