summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/calendar/caltemplate.cpp
blob: 6f545414d0638c7ffc89be6479099d45b3a77b5c (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
/* ============================================================
 * File  : caltemplate.cpp
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Date  : 2003-11-04
 * Description :
 *
 * Copyright 2003 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 *
 * 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 <tqvariant.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqbuttongroup.h>
#include <tqvbuttongroup.h>
#include <tqradiobutton.h>
#include <tqcheckbox.h>
#include <tqslider.h>
#include <tqlayout.h>
#include <tqtimer.h>
#include <tqfontdatabase.h>
#include <tqpaintdevicemetrics.h>
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqprinter.h>

// KDE includes.

#include <klocale.h>
#include <kstandarddirs.h>

// Local includes.

#include "caltemplate.h"
#include "calwidget.h"
#include "calsettings.h"
#include "calpainter.h"

namespace KIPICalendarPlugin
{

CalTemplate::CalTemplate(TQWidget* tqparent, const char* name)
           : TQWidget(tqparent, name)
{
    TQGridLayout *mainLayout = new TQGridLayout(this, 1, 1, 5, 5);

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

    previewSize_ = 300;

    TQGroupBox *boxPreview_ = new TQGroupBox( i18n("Preview"), this );
    boxPreview_->setColumnLayout(0, Qt::Vertical);
    boxPreview_->tqlayout()->setMargin( 5 );

    TQVBoxLayout *previewLayout = new TQVBoxLayout(boxPreview_->tqlayout());
    calWidget_ = new CalWidget(boxPreview_);
    previewLayout->addWidget(calWidget_, 0, TQt::AlignCenter);

    mainLayout->addWidget( boxPreview_, 0, 0 );

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

    TQGroupBox *gbox = new TQGroupBox( i18n("Settings"), this );
    gbox->setColumnLayout(0, Qt::Vertical );
    gbox->tqlayout()->setSpacing( 11 );
    gbox->tqlayout()->setMargin( 6 );
    TQVBoxLayout* gboxLayout = new TQVBoxLayout( gbox->tqlayout() );

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

    TQHBoxLayout *htqlayout = new TQHBoxLayout( 0, 0, 5 );
    htqlayout->addWidget( new TQLabel(i18n("Paper size:"), gbox) );

    comboPaperSize_ = new TQComboBox(false, gbox);
    htqlayout->addWidget(comboPaperSize_);
    gboxLayout->addLayout( htqlayout );

    TQStringList paperSizes;
    paperSizes << "A4";
    paperSizes << "US Letter";
    comboPaperSize_->insertStringList(paperSizes);

    connect(comboPaperSize_, TQT_SIGNAL(activated(int)),
            TQT_SLOT(slotParamsChanged()));

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

    btnGroupImagePos_ = new TQVButtonGroup(i18n("Image Position"), gbox);
    btnGroupImagePos_->setRadioButtonExclusive(true);

    // Insert the buttons in the order Top, Left, Right so that they
    // get the corresponding ids
    TQRadioButton* radioBtn = new TQRadioButton(i18n("Top"), btnGroupImagePos_);
    radioBtn->setChecked(true);
    Q_ASSERT(btnGroupImagePos_->id( radioBtn ) == CalParams::Top);

    radioBtn = new TQRadioButton(i18n("Left"), btnGroupImagePos_);
    Q_ASSERT(btnGroupImagePos_->id( radioBtn ) == CalParams::Left);

    radioBtn = new TQRadioButton(i18n("Right"), btnGroupImagePos_);
    Q_ASSERT(btnGroupImagePos_->id( radioBtn ) == CalParams::Right);

    gboxLayout->addWidget( btnGroupImagePos_ );

    connect(btnGroupImagePos_, TQT_SIGNAL(clicked(int)),
            TQT_SLOT(slotParamsChanged()));

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

    checkBoxDrawLines_ = new TQCheckBox(i18n("Draw lines in calendar"), gbox);
    gboxLayout->addWidget( checkBoxDrawLines_ );
    checkBoxDrawLines_->setChecked(true);

    connect(checkBoxDrawLines_, TQT_SIGNAL(toggled(bool)),
            TQT_SLOT(slotParamsChanged()));

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

    htqlayout = new TQHBoxLayout( 0, 0, 5 );

    htqlayout->addWidget(new TQLabel(i18n("Image to text ratio:"), gbox));

    sliderRatio_ = new TQSlider(50,300,5,100,Qt::Horizontal,gbox);
    htqlayout->addWidget( sliderRatio_ );

    gboxLayout->addLayout( htqlayout );

    connect(sliderRatio_, TQT_SIGNAL(valueChanged(int)),
            TQT_SLOT(slotParamsChanged()));

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

    htqlayout = new TQHBoxLayout( 0, 0, 5 );

    htqlayout->addWidget(new TQLabel(i18n("Font:"), gbox));

    comboFont_ = new TQComboBox(false, gbox);
    htqlayout->addWidget( comboFont_ );

    TQFontDatabase fontDB;
    TQStringList families(fontDB.tqfamilies());
    TQStringList smoothScalableFamilies;
    for (TQStringList::iterator it=families.begin(); it != families.end();
         ++it)
    {
        smoothScalableFamilies.append(*it);
    }
    comboFont_->insertStringList(smoothScalableFamilies);

    // fetch and set the default font selected in the combo.
    TQFont f;
    comboFont_->setCurrentText( f.family() );


    gboxLayout->addLayout( htqlayout );

    connect(comboFont_, TQT_SIGNAL(activated(int)),
            TQT_SLOT(slotParamsChanged()));


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

    gboxLayout->addItem(new TQSpacerItem(5,10,TQSizePolicy::Minimum,
                                        TQSizePolicy::Expanding));

    mainLayout->addWidget( gbox, 0, 1 );

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

    timer_ = new TQTimer(this);

    connect(timer_, TQT_SIGNAL(timeout()),
            TQT_SLOT(slotUpdatePreview()));

    timer_->start(0,true);
}

CalTemplate::~CalTemplate()
{
    delete timer_;
}


void CalTemplate::slotParamsChanged()
{
    timer_->start(10, true);
}

void CalTemplate::slotUpdatePreview()
{
    if (timer_->isActive()) return;

    CalParams& params = CalSettings::instance()->calParams;

    TQString paperSize = comboPaperSize_->currentText();
    if (paperSize == "A4") {
        params.paperWidth  = 210;
        params.paperHeight = 297;
        params.pageSize    = KPrinter::A4;
    }
    else if (paperSize == "US Letter") {
        params.paperWidth  = 216;
        params.paperHeight = 279;
        params.pageSize    = KPrinter::Letter;
    }

    const int imgPos = btnGroupImagePos_->selectedId();

    if (imgPos == CalParams::Top) {
        params.imgPos = CalParams::Top;

        float zoom = TQMIN((float)previewSize_/params.paperWidth,
                          (float)previewSize_/params.paperHeight);
        params.width  = (int)(params.paperWidth  * zoom);
        params.height = (int)(params.paperHeight * zoom);

    }
    else if (imgPos == CalParams::Left) {
        params.imgPos = CalParams::Left;

        float zoom = TQMIN((float)previewSize_/params.paperWidth,
                          (float)previewSize_/params.paperHeight);
        params.width  = (int)(params.paperHeight  * zoom);
        params.height = (int)(params.paperWidth   * zoom);

    }
    else {
        params.imgPos = CalParams::Right;

        float zoom = TQMIN((float)previewSize_/params.paperWidth,
                          (float)previewSize_/params.paperHeight);
        params.width  = (int)(params.paperHeight  * zoom);
        params.height = (int)(params.paperWidth   * zoom);
    }

    params.drawLines = checkBoxDrawLines_->isChecked();
    params.ratio     = sliderRatio_->value();
    params.baseFont  = TQFont(comboFont_->currentText());
    calWidget_->recreate();
}

}  // NameSpace KIPICalendarPlugin

#include "caltemplate.moc"