summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/calendar/calwizard.cpp
blob: 749183c8a0dff8dc76c72b5113ff569d2a4c4190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* ============================================================
 * File  : calwizard.cpp
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 *         Tom Albers <tomalbers@kde.nl>
 * Date  : 2003-11-03
 * Description :
 *
 * Copyright 2003 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright 2006 by Tom Albers <tomalbers@kde.nl>
 *
 * 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.
 *
 * ============================================================ */

// TQt includes.

#include <tqlabel.h>
#include <tqdatetime.h>
#include <tqstringlist.h>
#include <tqtimer.h>
#include <tqpainter.h>
#include <tqprogressbar.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqframe.h>
#include <tqpixmap.h>
#include <tqvbox.h>

// KDE includes.

#include <kglobal.h>
#include <klocale.h>
#include <kprinter.h>
#include <kapplication.h>
#include <khelpmenu.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>
#include <kdeversion.h>
#include <kcalendarsystem.h>
#include <klineedit.h>

// LibKipi includes.

#include <libkipi/interface.h>

// Local includes.

#include "kpaboutdata.h"
#include "pluginsversion.h"
#include "caltemplate.h"
#include "calselect.h"
#include "calsettings.h"
#include "calpainter.h"
#include "calwizard.h"
#include "calwizard.moc"
#include "calevents.h"

namespace KIPICalendarPlugin
{

CalWizard::CalWizard( KIPI::Interface* interface, TQWidget *parent )
         : KWizard(parent, 0, false, TQt::WDestructiveClose),
           interface_( interface )
{
    cSettings_ = new CalSettings();

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

    wTemplate_ = new CalTemplate(this, "wTemplate");
    addPage(wTemplate_, i18n("Create Template for Calendar"));
    setHelpEnabled(wTemplate_, true);

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

    wEvents_ = new CalEvents(this, "wEvents");
    addPage(wEvents_, i18n("Choose the events to show on the Calendar"));
    setHelpEnabled(wEvents_, true);

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

    wSelect_ = new CalSelect( interface, this, "wSelect");
    addPage(wSelect_, i18n("Select Year & Images"));
    setHelpEnabled(wSelect_, true);

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

    wPrint_ = new TQVBox(this, "wPrint");

    wPrintLabel_ = new TQLabel(wPrint_, "wPrint");
    wPrintLabel_->setIndent(20);

    wPrint_->setStretchFactor(wPrintLabel_, 2);

    addPage(wPrint_, i18n("Print"));
    setHelpEnabled(wPrint_, true);

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

    wFinish_ = new TQWidget(this, "wFinish");

    TQVBoxLayout *wFinishLayout = new TQVBoxLayout(wFinish_, 6, 11);

    wFinishLabel_ = new TQLabel(wFinish_);
    wFinishLayout->addWidget(wFinishLabel_);

    TQHBoxLayout *hboxtqlayout = new TQHBoxLayout(0, 5, 5);
    hboxtqlayout->addWidget(new TQLabel(i18n("Current Page"), wFinish_));
    wFinishProgressCurrent_ = new TQProgressBar(wFinish_);
    hboxtqlayout->addWidget(wFinishProgressCurrent_);
    wFinishLayout->addLayout(hboxtqlayout);

    hboxtqlayout = new TQHBoxLayout(0, 5, 5);
    hboxtqlayout->addWidget(new TQLabel(i18n("Total Pages"), wFinish_));
    wFinishProgressTotal_ = new TQProgressBar(wFinish_);
    hboxtqlayout->addWidget(wFinishProgressTotal_);
    wFinishLayout->addLayout(hboxtqlayout);

    wFinishLayout->addStretch();

    addPage(wFinish_, i18n("Printing"));
    setHelpEnabled(wFinish_, true);

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

    // About data and help button.

    m_about = new KIPIPlugins::KPAboutData(I18N_NOOP("Calendar"),
                                           0,
                                           KAboutData::License_GPL,
                                           I18N_NOOP("A Kipi plugin to create a calendar"),
                                           "(c) 2003-2004, Renchi Raju, (c) 2006 Tom Albers");

    m_about->addAuthor("Tom Albers", I18N_NOOP("Author and maintainer"),
                       "tomalbers@kde.nl");

    m_about->addAuthor("Renchi Raju", I18N_NOOP("Former Author and maintainer"),
                       "renchi@pooh.tam.uiuc.edu");

    m_helpButton = helpButton();
    KHelpMenu* helpMenu = new KHelpMenu(this, m_about, false);
    helpMenu->menu()->removeItemAt(0);
    helpMenu->menu()->insertItem(i18n("Plugin Handbook"), this, TQT_SLOT(slotHelp()), 0, -1, 0);
    m_helpButton->setPopup( helpMenu->menu() );

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

    printer_  = 0;
    painter_  = 0;

    formatter_ = new CalFormatter();

    connect(this, TQT_SIGNAL(selected(const TQString&)),
            TQT_SLOT(slotPageSelected(const TQString&)));

    setCaption(i18n("Create Calendar"));
}

CalWizard::~CalWizard()
{
    if (!cb_.isNull()) delete cb_;

    if (painter_) delete painter_;
    if (printer_) delete printer_;
    delete cSettings_;

    delete m_about;

    delete formatter_;
}

void CalWizard::slotHelp()
{
    KApplication::kApplication()->invokeHelp("calendar", "kipi-plugins");
}

void CalWizard::slotPageSelected(const TQString&)
{

    if (currentPage() == wPrint_) {

        totPages_ = 0;
        currPage_ = 0;
        monthNumbers_.clear();
        monthImages_.clear();
        KURL image;
        TQString month;
        TQStringList printList;
        TQDate d;
        KGlobal::locale()->calendar()->setYMD(d, cSettings_->getYear(), 1, 1);
        for (int i=1; i<=KGlobal::locale()->calendar()->monthsInYear(d); i++) {
#if KDE_IS_VERSION(3,2,0)
            month = KGlobal::locale()->calendar()->monthName(i, cSettings_->getYear(), false);
#else
            month = KGlobal::locale()->monthName(i);
#endif
            image = cSettings_->getImage(i);
            if (!image.isEmpty()) {
                monthNumbers_.append(i);
                monthImages_.append(image);
                printList.append(month);
            }
        }

        if (!monthNumbers_.empty()) {
            TQString year = TQString::number(cSettings_->getYear());

            TQString extra;
            if ((KGlobal::locale()->calendar()->month(TQDate::tqcurrentDate()) >= 6 &&
                 KGlobal::locale()->calendar()->year(TQDate::tqcurrentDate()) == cSettings_->getYear()) ||
                KGlobal::locale()->calendar()->year(TQDate::tqcurrentDate()) > cSettings_->getYear())
                extra = "<br><br><b>"+i18n("Please note that you are making a "
                        "calendar for<br>the current year or a year in the "
                        "past.")+"</b>";

            KApplication::startServiceByName("KJobViewer");
            TQString extra2 = i18n("<br><br>You can see KJobViewer is already started. "
                    "After the plugin has prepared the calendar, it is passed to "
                    "the PDF printer. In the KJobViewer you can see the progress "
                    "of that part of the generation of the calendar.");

            wPrintLabel_->setText(i18n("Click Next to start Printing<br><br>"
                                       "Following months will be printed for year %1:").tqarg(year)
                                  + TQString("<br>")
                             + printList.join(" - ") + extra + extra2);
            wPrintLabel_->setTextFormat(TQt::RichText);

            setNextEnabled(wPrint_, true);
        }
        else {
            wPrintLabel_->setText(i18n("No valid images selected for months<br>"
                                       "Click Back to select images"));
            setNextEnabled(wPrint_, false);
        }
    }

    else if (currentPage() == wFinish_) {

        wFinishLabel_->clear();
        wFinishProgressTotal_->reset();
        wFinishProgressCurrent_->reset();

        setBackEnabled(wFinish_, false);
        setFinishEnabled(wFinish_, false);

        // Set printer settings ---------------------------------------

        if (!printer_)
            printer_ = new KPrinter(false);
#if KDE_IS_VERSION(3,2,0)
        printer_->setUsePrinterResolution(true);
#endif

        // TODO: Let user choose resolutions
        //, TQPrinter::HighResolution);

        CalParams& params = cSettings_->calParams;

        // Qt::Orientation
        switch (params.imgPos) {
        case(CalParams::Top): {
            printer_->setOrientation(KPrinter::Portrait);
            break;
        }
        default:
            printer_->setOrientation(KPrinter::Landscape);
        }

        // PageSize
        printer_->setPageSize(params.pageSize);

        if (printer_->setup(this)) {

            // Start printing --------------------------------------------

            painter_ = new TQPainter(printer_);
            totPages_ = monthImages_.count();
            currPage_ = -1;
            formatter_->init(cSettings_->getYear(), wEvents_->ohFileEdit->text(), wEvents_->fhFileEdit->text());
            slotPrintOnePage();

        }
        else {
            wFinishLabel_->setText(i18n("Printing Cancelled"));
            setBackEnabled(wFinish_, true);
        }

    }

}

void CalWizard::slotPrintOnePage()
{
    if (monthNumbers_.empty()) {
        wFinishProgressTotal_->setProgress(totPages_,totPages_);
        painter_->end();
        delete painter_;
        painter_ = 0;
        setBackEnabled(wFinish_, true);
        setFinishEnabled(wFinish_, true);
        wFinishLabel_->setText(i18n("Printing Complete"));
        return;
    }

    int     month(monthNumbers_.first());
    KURL image(monthImages_.first());
    monthNumbers_.pop_front();
    monthImages_.pop_front();

    TQString yearName = TQString::number(cSettings_->getYear());

#if KDE_IS_VERSION(3,2,0)
    wFinishLabel_->setText(i18n("Printing Calendar Page for %1 of %2")
                    .tqarg(KGlobal::locale()->calendar()->monthName(month, cSettings_->getYear(), false))
                    .tqarg(yearName));
#else
    wFinishLabel_->setText(i18n("Printing Calendar Page for %1 of %2")
                     .tqarg(KGlobal::locale()->monthName(month)).
                     .tqarg(yearName));
#endif

    currPage_++;
    if (currPage_ != 0)
        printer_->newPage();
    wFinishProgressTotal_->setProgress(currPage_,totPages_);

    int angle = interface_->info( image ).angle();

    cb_ = new CalBlockPainter(TQT_TQOBJECT(this), cSettings_->getYear(), month,
                              image, angle, formatter_, painter_);

    connect(cb_, TQT_SIGNAL(signalCompleted()),
            TQT_SLOT(slotPrintOnePage()));
    connect(cb_, TQT_SIGNAL(signalProgress(int,int)),
            wFinishProgressCurrent_, TQT_SLOT(setProgress(int,int)));
}

}  // NameSpace KIPICalendarPlugin