wizard.cpp 12.4 KB
Newer Older
Gilles Caulier's avatar
Gilles Caulier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.digikam.org
 *
 * Date        : 2006-04-04
 * Description : A KIPI plugin to generate HTML image galleries
 *
 * Copyright (C) 2006-2010 by Aurelien Gateau <aurelien dot gateau at free.fr>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */
22

Gilles Caulier's avatar
Gilles Caulier committed
23
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
24

25
26
#include "wizard.moc"

27
// Qt includes
28
29
30
31

#include <QCheckBox>
#include <QLabel>

32
// KDE includes
33
34

#include <kapplication.h>
35
#include <kcombobox.h>
36
37
38
39
40
#include <kconfigdialogmanager.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klistwidget.h>
#include <klocale.h>
41
#include <kmenu.h>
42
43
#include <ktextbrowser.h>
#include <kurlrequester.h>
Gilles Caulier's avatar
Gilles Caulier committed
44
45
46
#include <ktoolinvocation.h>
#include <kpushbutton.h>
#include <khelpmenu.h>
47

48
// KIPI includes
49

50
#include <libkipi/imagecollectionselector.h>
51
#include <libkipi/interface.h>
52

53
// Local includes
54

55
#include "abstractthemeparameter.h"
56
#include "galleryinfo.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
57
#include "invisiblebuttongroup.h"
58
#include "kpaboutdata.h"
Gilles Caulier's avatar
Gilles Caulier committed
59
#include "kpversion.h"
60
#include "theme.h"
61
62
#include "ui_imagesettingspage.h"
#include "ui_outputpage.h"
63
64
65
#include "ui_themepage.h"
#include "ui_themeparameterspage.h"

Gilles Caulier's avatar
Gilles Caulier committed
66
67
namespace KIPIHTMLExport
{
68

Gilles Caulier's avatar
Gilles Caulier committed
69
70
class ThemeListBoxItem : public QListWidgetItem
{
71
public:
Gilles Caulier's avatar
Gilles Caulier committed
72
73
74
75
76
77

    ThemeListBoxItem(QListWidget* const list, Theme::Ptr theme)
        : QListWidgetItem(theme->name(), list), 
          mTheme(theme)
    {
    }
78

79
    Theme::Ptr mTheme;
80
81
};

Gilles Caulier's avatar
Gilles Caulier committed
82
// ----------------------------------------------------------------------------------------------
83
84

template <class Ui_Class>
Gilles Caulier's avatar
Gilles Caulier committed
85
86
87

class WizardPage : public QWidget, public Ui_Class
{
88
public:
Gilles Caulier's avatar
Gilles Caulier committed
89
90
91
92

    WizardPage(KAssistantDialog* const dialog, const QString& title)
        : QWidget(dialog)
    {
93
        this->setupUi(this);
94
95
96
        layout()->setMargin(0);
        mPage = dialog->addPage(this, title);
    }
97

Gilles Caulier's avatar
Gilles Caulier committed
98
99
    KPageWidgetItem* page() const
    {
100
101
        return mPage;
    }
102
103

private:
Gilles Caulier's avatar
Gilles Caulier committed
104

105
    KPageWidgetItem* mPage;
106
107
};

Gilles Caulier's avatar
Gilles Caulier committed
108
// ----------------------------------------------------------------------------------------------
109

Gilles Caulier's avatar
Gilles Caulier committed
110
typedef WizardPage<Ui_ThemePage>           ThemePage;
111
typedef WizardPage<Ui_ThemeParametersPage> ThemeParametersPage;
Gilles Caulier's avatar
Gilles Caulier committed
112
typedef WizardPage<Ui_OutputPage>          OutputPage;
113

Gilles Caulier's avatar
Gilles Caulier committed
114
115
class ImageSettingsPage : public WizardPage<Ui_ImageSettingsPage>
{
Aurélien Gâteau's avatar
Aurélien Gâteau committed
116
public:
Gilles Caulier's avatar
Gilles Caulier committed
117
118
119

    ImageSettingsPage(KAssistantDialog* const dialog, const QString& title)
        : WizardPage<Ui_ImageSettingsPage>(dialog, title)
120
121
122
123
124
125
    {
        InvisibleButtonGroup* group = new InvisibleButtonGroup(this);
        group->setObjectName("kcfg_useOriginalImageAsFullImage");
        group->addButton(mSaveImageButton, int(false));
        group->addButton(mUseOriginalImageButton, int(true));
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
126
127
};

Gilles Caulier's avatar
Gilles Caulier committed
128
// ----------------------------------------------------------------------------------------------
129

Gilles Caulier's avatar
Gilles Caulier committed
130
131
132
133
struct Wizard::Private
{
    GalleryInfo*                    mInfo;
    KConfigDialogManager*           mConfigManager;
134

Gilles Caulier's avatar
Gilles Caulier committed
135
136
137
138
139
140
    ImageCollectionSelector*        mCollectionSelector;
    KPageWidgetItem*                mCollectionSelectorPage;
    ThemePage*                      mThemePage;
    ThemeParametersPage*            mThemeParametersPage;
    ImageSettingsPage*              mImageSettingsPage;
    OutputPage*                     mOutputPage;
141

Gilles Caulier's avatar
Gilles Caulier committed
142
    QMap<QByteArray, QWidget*>      mThemeParameterWidgetFromName;
143

Gilles Caulier's avatar
Gilles Caulier committed
144
145
146
    void initThemePage()
    {
        KListWidget* listWidget = mThemePage->mThemeList;
147
148
        Theme::List list=Theme::getList();
        Theme::List::ConstIterator it=list.constBegin(), end=list.constEnd();
Gilles Caulier's avatar
Gilles Caulier committed
149
150
151
152
153
154
155
        for (; it!=end; ++it)
        {
            Theme::Ptr theme       = *it;
            ThemeListBoxItem* item = new ThemeListBoxItem(listWidget, theme);

            if ( theme->internalName()==mInfo->theme() )
            {
156
157
158
159
160
                listWidget->setCurrentItem(item);
            }
        }
    }

Gilles Caulier's avatar
Gilles Caulier committed
161
162
    void fillThemeParametersPage(Theme::Ptr theme)
    {
163
164
        // Create a new content page
        delete mThemeParametersPage->content;
Gilles Caulier's avatar
Gilles Caulier committed
165
        QWidget* content              = new QWidget;
166
167
168
169
170
171
172
173
174
175
176
177
178
        mThemeParametersPage->content = content;
        mThemeParametersPage->scrollArea->setWidget(mThemeParametersPage->content);
        mThemeParametersPage->scrollArea->viewport()->setAutoFillBackground(false);
        content->setAutoFillBackground(false);
        mThemeParameterWidgetFromName.clear();

        // Create layout. We need to recreate it every time, to get rid of
        // spacers
        QGridLayout* layout = new QGridLayout(content);
        layout->setMargin(0);
        layout->setSpacing(KDialog::spacingHint());

        // Create widgets
Gilles Caulier's avatar
Gilles Caulier committed
179
180
181
182
183
184
185
        Theme::ParameterList parameterList      = theme->parameterList();
        QString themeInternalName               = theme->internalName();
        Theme::ParameterList::ConstIterator it  = parameterList.constBegin(),
                                            end = parameterList.constEnd();

        for (; it!=end; ++it)
        {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
            AbstractThemeParameter* themeParameter = *it;
            QByteArray internalName = themeParameter->internalName();
            QString value = mInfo->getThemeParameterValue(
                themeInternalName,
                internalName,
                themeParameter->defaultValue());

            QString name = themeParameter->name();
            name = i18nc("'%1' is a label for a theme parameter", "%1:", name);

            QLabel* label = new QLabel(name, content);
            label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
            QWidget* widget = themeParameter->createWidget(content, value);
            label->setBuddy(widget);

            int row = layout->rowCount();
            layout->addWidget(label, row, 0);

Gilles Caulier's avatar
Gilles Caulier committed
204
205
            if (widget->sizePolicy().expandingDirections() & Qt::Horizontal)
            {
206
207
                // Widget wants full width
                layout->addWidget(widget, row, 1, 1, 2);
Gilles Caulier's avatar
Gilles Caulier committed
208
209
210
            }
            else
            {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
                // Widget doesn't like to be stretched, add a spacer next to it
                layout->addWidget(widget, row, 1);
                QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
                layout->addItem(spacer, row, 2);
            }

            mThemeParameterWidgetFromName[internalName] = widget;
        }

        // Add spacer at the end, so that widgets aren't spread on the whole
        // parent height
        QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
        layout->addItem(spacer, layout->rowCount(), 0);
    }
225
226
};

Gilles Caulier's avatar
Gilles Caulier committed
227
Wizard::Wizard(QWidget* const parent, GalleryInfo* const info)
Gilles Caulier's avatar
Gilles Caulier committed
228
    : KPWizardDialog(parent)
229
{
Gilles Caulier's avatar
Gilles Caulier committed
230
231
    d        = new Private;
    d->mInfo = info;
232

Gilles Caulier's avatar
Gilles Caulier committed
233
234
235
236
237
    setCaption(i18n("Export image collections to HTML pages"));

    // ---------------------------------------------------------------
    // About data and help button.

Gilles Caulier's avatar
Gilles Caulier committed
238
239
240
241
242
243
244
    KPAboutData* about = new KPAboutData(
                                ki18n("HTML Export"),
                                QByteArray(),
                                KAboutData::License_GPL,
                                ki18n("A KIPI plugin to export image collections to HTML pages"),
                                ki18n("(c) 2006-2009, Aurelien Gateau\n"
                                      "(c) 2010, Gianluca Urgese"));
245

Gilles Caulier's avatar
Gilles Caulier committed
246
    about->addAuthor(
247
248
249
        ki18n("Gianluca Urgese"),
        ki18n("Maintainer"),
        "giasone.82@gmail.com");
250

Gilles Caulier's avatar
Gilles Caulier committed
251
    about->addAuthor(
252
        ki18n("Aurelien Gateau"),
253
254
        ki18n("Former Author and Maintainer"),
        "agateau@kde.org");
255

256
    about->setHandbookEntry("htmlexport");
Gilles Caulier's avatar
Gilles Caulier committed
257
    setAboutData(about);
Gilles Caulier's avatar
Gilles Caulier committed
258
259
260

    // ---------------------------------------------------------------

Gilles Caulier's avatar
Gilles Caulier committed
261
    d->mCollectionSelector     = iface()->imageCollectionSelector(this);
262
263
    d->mCollectionSelectorPage = addPage(d->mCollectionSelector, i18n("Collection Selection"));
    updateCollectionSelectorPageValidity();
Gilles Caulier's avatar
Gilles Caulier committed
264

265
    connect(d->mCollectionSelector, SIGNAL(selectionChanged()),
Gilles Caulier's avatar
Gilles Caulier committed
266
            this, SLOT(updateCollectionSelectorPageValidity()));
267

Gilles Caulier's avatar
Gilles Caulier committed
268
    d->mThemePage = new ThemePage(this, i18n("Theme"));
269
    d->initThemePage();
Gilles Caulier's avatar
Gilles Caulier committed
270

271
    connect(d->mThemePage->mThemeList, SIGNAL(itemSelectionChanged()),
Gilles Caulier's avatar
Gilles Caulier committed
272
            this, SLOT(slotThemeSelectionChanged()) );
273

274
    d->mThemeParametersPage = new ThemeParametersPage(this, i18n("Theme Parameters"));
Gilles Caulier's avatar
Gilles Caulier committed
275
276
    d->mImageSettingsPage   = new ImageSettingsPage(this, i18n("Image Settings"));
    d->mOutputPage          = new OutputPage(this, i18n("Output"));
277
    d->mOutputPage->kcfg_destUrl->setMode(KFile::Directory);
278

Laurent Montel's avatar
Laurent Montel committed
279
    connect(d->mOutputPage->kcfg_destUrl, SIGNAL(textChanged(QString)),
Gilles Caulier's avatar
Gilles Caulier committed
280
            this, SLOT(updateFinishPageValidity()) );
281

Gilles Caulier's avatar
Gilles Caulier committed
282
    d->mConfigManager = new KConfigDialogManager(this, d->mInfo);
283
    d->mConfigManager->updateWidgets();
284

285
286
287
288
    // Set page states
    // Pages can only be disabled after they have *all* been added!
    slotThemeSelectionChanged();
    updateFinishPageValidity();
289
290
}

Gilles Caulier's avatar
Gilles Caulier committed
291
292
Wizard::~Wizard()
{
293
    delete d;
294
295
}

Gilles Caulier's avatar
Gilles Caulier committed
296
297
void Wizard::updateFinishPageValidity()
{
298
    setValid(d->mOutputPage->page(), !d->mOutputPage->kcfg_destUrl->url().isEmpty());
299
300
301
}


Gilles Caulier's avatar
Gilles Caulier committed
302
303
void Wizard::updateCollectionSelectorPageValidity()
{
304
    setValid(d->mCollectionSelectorPage, !d->mCollectionSelector->selectedImageCollections().empty());
305
306
}

Gilles Caulier's avatar
Gilles Caulier committed
307
308
309
310
void Wizard::slotThemeSelectionChanged()
{
    KListWidget* listWidget = d->mThemePage->mThemeList;
    KTextBrowser* browser   = d->mThemePage->mThemeInfo;
311

Gilles Caulier's avatar
Gilles Caulier committed
312
313
    if (listWidget->currentItem())
    {
314
315
        Theme::Ptr theme=static_cast<ThemeListBoxItem*>(listWidget->currentItem())->mTheme;

Gilles Caulier's avatar
Gilles Caulier committed
316
317
318
319
320
        QString url    = theme->authorUrl();
        QString author = theme->authorName();
        bool allowNonsquareThumbnails=theme->allowNonsquareThumbnails();
        if (!url.isEmpty())
        {
321
322
323
            author=QString("<a href='%1'>%2</a>").arg(url).arg(author);
        }

Gilles Caulier's avatar
Gilles Caulier committed
324
325
326
327
328
        QString preview = theme->previewUrl();
        QString image   = "";

        if (!preview.isEmpty())
        {
Gianluca Urgese's avatar
Gianluca Urgese committed
329
330
331
            image=QString("<img src='%1/%2' /><br/><br/>").arg(theme->directory(), theme->previewUrl());
        }

332
        QString txt=
Gianluca Urgese's avatar
Gianluca Urgese committed
333
334
            image +
            QString("<b>%3</b><br/><br/>%4<br/><br/>").arg(theme->name(), theme->comment())
335
            + i18n("Author: %1", author);
Gianluca Urgese's avatar
Gianluca Urgese committed
336
        browser->setHtml(txt);
337
338
339
340
341
342
        setValid(d->mThemePage->page(), true);

        // Enable theme parameter page if there is any parameter
        Theme::ParameterList parameterList = theme->parameterList();
        setAppropriate(d->mThemeParametersPage->page(), parameterList.size() > 0);

Gilles Caulier's avatar
Gilles Caulier committed
343
344
345
346
        d->mImageSettingsPage->kcfg_thumbnailSquare->setEnabled(allowNonsquareThumbnails);
        
        if (!allowNonsquareThumbnails)
            d->mImageSettingsPage->kcfg_thumbnailSquare->setChecked(true);
347

348
        d->fillThemeParametersPage(theme);
Gilles Caulier's avatar
Gilles Caulier committed
349
350
351
    }
    else
    {
352
353
354
        browser->clear();
        setValid(d->mThemePage->page(), false);
    }
355
356
357
358
359
}

/**
 * Update mInfo
 */
Gilles Caulier's avatar
Gilles Caulier committed
360
361
void Wizard::accept()
{
362
363
    d->mInfo->mCollectionList=d->mCollectionSelector->selectedImageCollections();

Gilles Caulier's avatar
Gilles Caulier committed
364
    Theme::Ptr theme          = static_cast<ThemeListBoxItem*>(d->mThemePage->mThemeList->currentItem())->mTheme;
365
366
367
    QString themeInternalName = theme->internalName();
    d->mInfo->setTheme(themeInternalName);

Gilles Caulier's avatar
Gilles Caulier committed
368
369
370
371
372
    Theme::ParameterList parameterList      = theme->parameterList();
    Theme::ParameterList::ConstIterator it  = parameterList.constBegin(),
                                        end = parameterList.constEnd();
    for (; it!=end; ++it)
    {
373
        AbstractThemeParameter* themeParameter = *it;
Gilles Caulier's avatar
Gilles Caulier committed
374
375
376
        QByteArray parameterInternalName       = themeParameter->internalName();
        QWidget* widget                        = d->mThemeParameterWidgetFromName[parameterInternalName];
        QString value                          = themeParameter->valueFromWidget(widget);
377
378
379
380
381
382
383
384
385
386

        d->mInfo->setThemeParameterValue(
            themeInternalName,
            parameterInternalName,
            value);
    }

    d->mConfigManager->updateSettings();

    KAssistantDialog::accept();
387
388
}

389
} // namespace KIPIHTMLExport