42
42
#include "lib/memoryutils.h"
43
43
#include "lib/paintutils.h"
48
QToolButton* createToolButton() {
49
QToolButton* button = new QToolButton;
50
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
48
QToolButton* createToolButton()
50
QToolButton* button = new QToolButton;
51
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
56
56
struct SaveBarPrivate {
58
KActionCollection* mActionCollection;
59
QWidget* mSaveBarWidget;
60
QWidget* mTopRowWidget;
61
QToolButton* mUndoButton;
62
QToolButton* mRedoButton;
63
QToolButton* mSaveCurrentUrlButton;
64
QToolButton* mSaveAsButton;
65
QToolButton* mSaveAllButton;
66
QToolButton* mSaveAllFullScreenButton;
67
QLabel* mMessageLabel;
68
QLabel* mActionsLabel;
69
QFrame* mTooManyChangesFrame;
73
void createTooManyChangesFrame() {
74
mTooManyChangesFrame = new QFrame;
77
QLabel* iconLabel = new QLabel;
78
QPixmap pix = KIconLoader::global()->loadIcon(
79
"dialog-warning", KIconLoader::Dialog, KIconLoader::SizeSmall);
80
iconLabel->setPixmap(pix);
83
QLabel* textLabel = new QLabel;
85
i18n("You have modified many images. To avoid memory problems, you should save your changes.")
88
mSaveAllFullScreenButton = createToolButton();
91
QHBoxLayout* layout = new QHBoxLayout(mTooManyChangesFrame);
93
layout->addWidget(iconLabel);
94
layout->addWidget(textLabel);
95
layout->addWidget(mSaveAllFullScreenButton);
96
mTooManyChangesFrame->hide();
99
KColorScheme scheme(mSaveBarWidget->palette().currentColorGroup(), KColorScheme::Window);
100
QColor warningBackgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
101
QColor warningBorderColor = PaintUtils::adjustedHsv(warningBackgroundColor, 0, 150, 0);
102
QColor warningColor = scheme.foreground(KColorScheme::NegativeText).color();
106
" background-color: %1;"
107
" border: 1px solid %2;"
108
" border-radius: 4px;"
116
.arg(warningBackgroundColor.name())
117
.arg(warningBorderColor.name())
118
.arg(warningColor.name())
120
mTooManyChangesFrame->setStyleSheet(css);
123
void applyNormalStyleSheet() {
124
QColor bgColor = QToolTip::palette().base().color();
125
QColor borderColor = PaintUtils::adjustedHsv(bgColor, 0, 150, 0);
126
QColor fgColor = QToolTip::palette().text().color();
130
" background-color: %1;"
131
" border-top: 1px solid %2;"
132
" border-bottom: 1px solid %2;"
139
.arg(borderColor.name())
142
mSaveBarWidget->setStyleSheet(css);
145
void applyFullScreenStyleSheet() {
148
" background-color: #333;"
150
mSaveBarWidget->setStyleSheet(css);
154
void updateTooManyChangesFrame(const QList<KUrl>& list) {
155
qreal maxPercentageOfMemoryUsage = GwenviewConfig::percentageOfMemoryUsageWarning();
156
qulonglong maxMemoryUsage = MemoryUtils::getTotalMemory() * maxPercentageOfMemoryUsage;
157
qulonglong memoryUsage = 0;
158
Q_FOREACH(const KUrl& url, list) {
159
Document::Ptr doc = DocumentFactory::instance()->load(url);
160
memoryUsage += doc->memoryUsage();
163
mTooManyChangesFrame->setVisible(memoryUsage > maxMemoryUsage);
167
void updateTopRowWidget(const QList<KUrl>& lst) {
171
if (lst.contains(mCurrentUrl)) {
172
message = i18n("Current image modified");
177
if (lst.size() > 1) {
178
QString previous = i18n("Previous modified image");
179
QString next = i18n("Next modified image");
180
if (mCurrentUrl == lst[0]) {
183
links << QString("<a href='previous'>%1</a>").arg(previous);
185
if (mCurrentUrl == lst[lst.size() - 1]) {
188
links << QString("<a href='next'>%1</a>").arg(next);
195
message = i18np("One image modified", "%1 images modified", lst.size());
196
if (lst.size() > 1) {
197
links << QString("<a href='first'>%1</a>").arg(i18n("Go to first modified image"));
199
links << QString("<a href='first'>%1</a>").arg(i18n("Go to it"));
203
mSaveCurrentUrlButton->setVisible(lst.contains(mCurrentUrl));
204
mSaveAsButton->setVisible(lst.contains(mCurrentUrl));
205
mSaveAllButton->setVisible(lst.size() >= 1);
207
mMessageLabel->setText(message);
208
mActionsLabel->setText(links.join(" | "));
212
void updateWidgetSizes() {
213
QVBoxLayout* layout = static_cast<QVBoxLayout*>(mSaveBarWidget->layout());
214
int topRowHeight = mFullScreenMode ? 0 : mTopRowWidget->height();
215
int bottomRowHeight = mTooManyChangesFrame->isVisibleTo(mSaveBarWidget) ? mTooManyChangesFrame->sizeHint().height() : 0;
217
int height = 2 * layout->margin() + topRowHeight + bottomRowHeight;
218
if (topRowHeight > 0 && bottomRowHeight > 0) {
219
height += layout->spacing();
221
mSaveBarWidget->setFixedHeight(height);
222
that->setFixedHeight(height);
58
KActionCollection* mActionCollection;
59
QWidget* mSaveBarWidget;
60
QWidget* mTopRowWidget;
61
QToolButton* mUndoButton;
62
QToolButton* mRedoButton;
63
QToolButton* mSaveCurrentUrlButton;
64
QToolButton* mSaveAsButton;
65
QToolButton* mSaveAllButton;
66
QToolButton* mSaveAllFullScreenButton;
67
QLabel* mMessageLabel;
68
QLabel* mActionsLabel;
69
QFrame* mTooManyChangesFrame;
73
void createTooManyChangesFrame()
75
mTooManyChangesFrame = new QFrame;
78
QLabel* iconLabel = new QLabel;
79
QPixmap pix = KIconLoader::global()->loadIcon(
80
"dialog-warning", KIconLoader::Dialog, KIconLoader::SizeSmall);
81
iconLabel->setPixmap(pix);
84
QLabel* textLabel = new QLabel;
86
i18n("You have modified many images. To avoid memory problems, you should save your changes.")
89
mSaveAllFullScreenButton = createToolButton();
92
QHBoxLayout* layout = new QHBoxLayout(mTooManyChangesFrame);
94
layout->addWidget(iconLabel);
95
layout->addWidget(textLabel);
96
layout->addWidget(mSaveAllFullScreenButton);
97
mTooManyChangesFrame->hide();
100
KColorScheme scheme(mSaveBarWidget->palette().currentColorGroup(), KColorScheme::Window);
101
QColor warningBackgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
102
QColor warningBorderColor = PaintUtils::adjustedHsv(warningBackgroundColor, 0, 150, 0);
103
QColor warningColor = scheme.foreground(KColorScheme::NegativeText).color();
107
" background-color: %1;"
108
" border: 1px solid %2;"
109
" border-radius: 4px;"
117
.arg(warningBackgroundColor.name())
118
.arg(warningBorderColor.name())
119
.arg(warningColor.name())
121
mTooManyChangesFrame->setStyleSheet(css);
124
void applyNormalStyleSheet()
126
QColor bgColor = QToolTip::palette().base().color();
127
QColor borderColor = PaintUtils::adjustedHsv(bgColor, 0, 150, 0);
128
QColor fgColor = QToolTip::palette().text().color();
132
" background-color: %1;"
133
" border-top: 1px solid %2;"
134
" border-bottom: 1px solid %2;"
141
.arg(borderColor.name())
144
mSaveBarWidget->setStyleSheet(css);
147
void applyFullScreenStyleSheet()
151
" background-color: #333;"
153
mSaveBarWidget->setStyleSheet(css);
156
void updateTooManyChangesFrame(const QList<KUrl>& list)
158
qreal maxPercentageOfMemoryUsage = GwenviewConfig::percentageOfMemoryUsageWarning();
159
qulonglong maxMemoryUsage = MemoryUtils::getTotalMemory() * maxPercentageOfMemoryUsage;
160
qulonglong memoryUsage = 0;
161
Q_FOREACH(const KUrl & url, list) {
162
Document::Ptr doc = DocumentFactory::instance()->load(url);
163
memoryUsage += doc->memoryUsage();
166
mTooManyChangesFrame->setVisible(memoryUsage > maxMemoryUsage);
169
void updateTopRowWidget(const QList<KUrl>& lst)
174
if (lst.contains(mCurrentUrl)) {
175
message = i18n("Current image modified");
180
if (lst.size() > 1) {
181
QString previous = i18n("Previous modified image");
182
QString next = i18n("Next modified image");
183
if (mCurrentUrl == lst[0]) {
186
links << QString("<a href='previous'>%1</a>").arg(previous);
188
if (mCurrentUrl == lst[lst.size() - 1]) {
191
links << QString("<a href='next'>%1</a>").arg(next);
198
message = i18np("One image modified", "%1 images modified", lst.size());
199
if (lst.size() > 1) {
200
links << QString("<a href='first'>%1</a>").arg(i18n("Go to first modified image"));
202
links << QString("<a href='first'>%1</a>").arg(i18n("Go to it"));
206
mSaveCurrentUrlButton->setVisible(lst.contains(mCurrentUrl));
207
mSaveAsButton->setVisible(lst.contains(mCurrentUrl));
208
mSaveAllButton->setVisible(lst.size() >= 1);
210
mMessageLabel->setText(message);
211
mActionsLabel->setText(links.join(" | "));
214
void updateWidgetSizes()
216
QVBoxLayout* layout = static_cast<QVBoxLayout*>(mSaveBarWidget->layout());
217
int topRowHeight = mFullScreenMode ? 0 : mTopRowWidget->height();
218
int bottomRowHeight = mTooManyChangesFrame->isVisibleTo(mSaveBarWidget) ? mTooManyChangesFrame->sizeHint().height() : 0;
220
int height = 2 * layout->margin() + topRowHeight + bottomRowHeight;
221
if (topRowHeight > 0 && bottomRowHeight > 0) {
222
height += layout->spacing();
224
mSaveBarWidget->setFixedHeight(height);
227
228
SaveBar::SaveBar(QWidget* parent, KActionCollection* actionCollection)
228
229
: SlideContainer(parent)
229
, d(new SaveBarPrivate) {
231
d->mFullScreenMode = false;
232
d->mActionCollection = actionCollection;
233
d->mSaveBarWidget = new QWidget();
234
d->mSaveBarWidget->setObjectName( QLatin1String("saveBarWidget" ));
235
d->applyNormalStyleSheet();
237
d->mMessageLabel = new QLabel;
238
d->mMessageLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
240
d->mUndoButton = createToolButton();
241
d->mRedoButton = createToolButton();
242
d->mSaveCurrentUrlButton = createToolButton();
243
d->mSaveAsButton = createToolButton();
244
d->mSaveAllButton = createToolButton();
246
d->mActionsLabel = new QLabel;
247
d->mActionsLabel->setAlignment(Qt::AlignCenter);
248
d->mActionsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
250
d->createTooManyChangesFrame();
253
d->mTopRowWidget = new QWidget;
254
QHBoxLayout* rowLayout = new QHBoxLayout(d->mTopRowWidget);
255
rowLayout->addWidget(d->mMessageLabel);
256
rowLayout->addWidget(d->mUndoButton);
257
rowLayout->addWidget(d->mRedoButton);
258
rowLayout->addWidget(d->mActionsLabel);
259
rowLayout->addWidget(d->mSaveCurrentUrlButton);
260
rowLayout->addWidget(d->mSaveAsButton);
261
rowLayout->addWidget(d->mSaveAllButton);
262
rowLayout->setMargin(0);
265
QHBoxLayout* bottomRowLayout = new QHBoxLayout;
266
bottomRowLayout->addStretch();
267
bottomRowLayout->addWidget(d->mTooManyChangesFrame);
268
bottomRowLayout->addStretch();
270
// Gather everything together
271
QVBoxLayout* layout = new QVBoxLayout(d->mSaveBarWidget);
272
layout->addWidget(d->mTopRowWidget);
273
layout->addLayout(bottomRowLayout);
274
layout->setMargin(3);
275
layout->setSpacing(3);
279
setContent(d->mSaveBarWidget);
281
connect(DocumentFactory::instance(), SIGNAL(modifiedDocumentListChanged()),
282
SLOT(updateContent()) );
284
connect(d->mActionsLabel, SIGNAL(linkActivated(const QString&)),
285
SLOT(triggerAction(const QString&)) );
289
SaveBar::~SaveBar() {
294
void SaveBar::initActionDependentWidgets() {
295
d->mUndoButton->setDefaultAction(d->mActionCollection->action("edit_undo"));
296
d->mRedoButton->setDefaultAction(d->mActionCollection->action("edit_redo"));
297
d->mSaveCurrentUrlButton->setDefaultAction(d->mActionCollection->action("file_save"));
298
d->mSaveAsButton->setDefaultAction(d->mActionCollection->action("file_save_as"));
300
// FIXME: Not using an action for now
301
d->mSaveAllButton->setText(i18n("Save All"));
302
d->mSaveAllButton->setIcon(KIcon("document-save-all"));
303
connect(d->mSaveAllButton, SIGNAL(clicked()),
304
SIGNAL(requestSaveAll()) );
306
d->mSaveAllFullScreenButton->setText(i18n("Save All"));
307
connect(d->mSaveAllFullScreenButton, SIGNAL(clicked()),
308
SIGNAL(requestSaveAll()) );
310
int height = d->mUndoButton->sizeHint().height();
311
d->mTopRowWidget->setFixedHeight(height);
312
d->updateWidgetSizes();
316
void SaveBar::setFullScreenMode(bool value) {
317
d->mFullScreenMode = value;
318
d->mSaveAllFullScreenButton->setVisible(value);
320
d->applyFullScreenStyleSheet();
322
d->applyNormalStyleSheet();
328
void SaveBar::updateContent() {
329
QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
330
if (lst.size() == 0) {
335
if (d->mFullScreenMode) {
336
d->mTopRowWidget->hide();
338
d->mTopRowWidget->show();
339
d->updateTopRowWidget(lst);
342
d->updateTooManyChangesFrame(lst);
344
d->updateWidgetSizes();
345
if (d->mFullScreenMode && !d->mTooManyChangesFrame->isVisibleTo(d->mSaveBarWidget)) {
353
void SaveBar::triggerAction(const QString& action) {
354
QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
355
if (action == "first") {
357
} else if (action == "previous") {
358
int pos = lst.indexOf(d->mCurrentUrl);
362
} else if (action == "next") {
363
int pos = lst.indexOf(d->mCurrentUrl);
365
Q_ASSERT(pos < lst.size());
368
kWarning() << "Unknown action: " << action ;
373
void SaveBar::setCurrentUrl(const KUrl& url) {
374
d->mCurrentUrl = url;
230
, d(new SaveBarPrivate)
233
d->mFullScreenMode = false;
234
d->mActionCollection = actionCollection;
235
d->mSaveBarWidget = new QWidget();
236
d->mSaveBarWidget->setObjectName(QLatin1String("saveBarWidget"));
237
d->applyNormalStyleSheet();
239
d->mMessageLabel = new QLabel;
240
d->mMessageLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
242
d->mUndoButton = createToolButton();
243
d->mRedoButton = createToolButton();
244
d->mSaveCurrentUrlButton = createToolButton();
245
d->mSaveAsButton = createToolButton();
246
d->mSaveAllButton = createToolButton();
248
d->mActionsLabel = new QLabel;
249
d->mActionsLabel->setAlignment(Qt::AlignCenter);
250
d->mActionsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
252
d->createTooManyChangesFrame();
255
d->mTopRowWidget = new QWidget;
256
QHBoxLayout* rowLayout = new QHBoxLayout(d->mTopRowWidget);
257
rowLayout->addWidget(d->mMessageLabel);
258
rowLayout->addWidget(d->mUndoButton);
259
rowLayout->addWidget(d->mRedoButton);
260
rowLayout->addWidget(d->mActionsLabel);
261
rowLayout->addWidget(d->mSaveCurrentUrlButton);
262
rowLayout->addWidget(d->mSaveAsButton);
263
rowLayout->addWidget(d->mSaveAllButton);
264
rowLayout->setMargin(0);
267
QHBoxLayout* bottomRowLayout = new QHBoxLayout;
268
bottomRowLayout->addStretch();
269
bottomRowLayout->addWidget(d->mTooManyChangesFrame);
270
bottomRowLayout->addStretch();
272
// Gather everything together
273
QVBoxLayout* layout = new QVBoxLayout(d->mSaveBarWidget);
274
layout->addWidget(d->mTopRowWidget);
275
layout->addLayout(bottomRowLayout);
276
layout->setMargin(3);
277
layout->setSpacing(3);
279
setContent(d->mSaveBarWidget);
281
connect(DocumentFactory::instance(), SIGNAL(modifiedDocumentListChanged()),
282
SLOT(updateContent()));
284
connect(d->mActionsLabel, SIGNAL(linkActivated(QString)),
285
SLOT(triggerAction(QString)));
293
void SaveBar::initActionDependentWidgets()
295
d->mUndoButton->setDefaultAction(d->mActionCollection->action("edit_undo"));
296
d->mRedoButton->setDefaultAction(d->mActionCollection->action("edit_redo"));
297
d->mSaveCurrentUrlButton->setDefaultAction(d->mActionCollection->action("file_save"));
298
d->mSaveAsButton->setDefaultAction(d->mActionCollection->action("file_save_as"));
300
// FIXME: Not using an action for now
301
d->mSaveAllButton->setText(i18n("Save All"));
302
d->mSaveAllButton->setIcon(KIcon("document-save-all"));
303
connect(d->mSaveAllButton, SIGNAL(clicked()),
304
SIGNAL(requestSaveAll()));
306
d->mSaveAllFullScreenButton->setText(i18n("Save All"));
307
connect(d->mSaveAllFullScreenButton, SIGNAL(clicked()),
308
SIGNAL(requestSaveAll()));
310
int height = d->mUndoButton->sizeHint().height();
311
d->mTopRowWidget->setFixedHeight(height);
312
d->updateWidgetSizes();
315
void SaveBar::setFullScreenMode(bool value)
317
d->mFullScreenMode = value;
318
d->mSaveAllFullScreenButton->setVisible(value);
320
d->applyFullScreenStyleSheet();
322
d->applyNormalStyleSheet();
327
void SaveBar::updateContent()
329
QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
331
if (d->mFullScreenMode) {
332
d->mTopRowWidget->hide();
334
d->mTopRowWidget->show();
335
d->updateTopRowWidget(lst);
338
d->updateTooManyChangesFrame(lst);
340
d->updateWidgetSizes();
341
if (lst.isEmpty() || (d->mFullScreenMode && !d->mTooManyChangesFrame->isVisibleTo(d->mSaveBarWidget))) {
348
void SaveBar::triggerAction(const QString& action)
350
QList<KUrl> lst = DocumentFactory::instance()->modifiedDocumentList();
351
if (action == "first") {
353
} else if (action == "previous") {
354
int pos = lst.indexOf(d->mCurrentUrl);
358
} else if (action == "next") {
359
int pos = lst.indexOf(d->mCurrentUrl);
361
Q_ASSERT(pos < lst.size());
364
kWarning() << "Unknown action: " << action ;
368
void SaveBar::setCurrentUrl(const KUrl& url)
370
d->mCurrentUrl = url;