summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/calendar/calselect.cpp
blob: e3662a442c873ea4a27a9a170149dd4ab7516f89 (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
/* ============================================================
 * File  : calselect.cpp
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Date  : 2003-11-03
 * 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 <tqhgroupbox.h>
#include <tqlayout.h>
#include <tqspinbox.h>
#include <tqdatetime.h>
#include <tqlabel.h>
#include <tqframe.h>
#include <tqpixmap.h>

// KDE includes.

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kcalendarsystem.h>
#include <kstandarddirs.h>

// Local includes.

#include "calselect.h"
#include "calsettings.h"
#include "monthwidget.h"

#define MAX_MONTHS (13)

namespace KIPICalendarPlugin
{

CalSelect::CalSelect(KIPI::Interface* interface, TQWidget *parent, const char* name)
         : TQWidget(parent, name)
{
    mwVector_ = new TQPtrVector<MonthWidget>(MAX_MONTHS);
    monthBoxLayout_ = NULL;
    setupView( interface );
}

CalSelect::~CalSelect()
{
    delete mwVector_;
}

void CalSelect::setupView( KIPI::Interface* interface )
{
    TQVBoxLayout *mainLayout = new TQVBoxLayout(this, 6, 11);

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

    setCaption(i18n("Create Calendar"));
    TQHGroupBox *yearBox = new TQHGroupBox(i18n("Select Year"), this);
    yearBox->layout()->addItem(new TQSpacerItem(5,5,
                                               TQSizePolicy::Expanding,
                                               TQSizePolicy::Minimum));
    yearSpin_ = new TQSpinBox(TDEGlobal::locale()->calendar()->minValidYear(),
                             TDEGlobal::locale()->calendar()->maxValidYear(),
                             1,yearBox);
    yearSpin_->setValue(TDEGlobal::locale()->calendar()->year(TQDate::currentDate()));
    slotYearChanged(yearSpin_->value());

    connect(yearSpin_, TQT_SIGNAL(valueChanged(int)),
            TQT_SLOT(slotYearChanged(int)));

    mainLayout->addWidget(yearBox);

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

    TQGroupBox *monthBox = new TQGroupBox(i18n("Select Images"), this);
    monthBox->setColumnLayout(0, TQt::Vertical );
    monthBox->layout()->setSpacing( 6 );
    monthBox->layout()->setMargin( 11 );

    monthBoxLayout_ = new TQGridLayout(monthBox->layout());
    monthBoxLayout_->setAlignment( TQt::AlignCenter );

    KURL::List urlList;
    KIPI::ImageCollection images = interface->currentSelection();
    if ( images.isValid() && !images.images().isEmpty())
        urlList = images.images();

    TQDate d;
    TDEGlobal::locale()->calendar()->setYMD(d, yearSpin_->value(), 1, 1);
    unsigned int months = TDEGlobal::locale()->calendar()->monthsInYear(d);
    // span the monthWidgets over 2 rows. inRow should usually be 6 or 7 (for 12 or 13 months)
    int inRow = (months / 2) + ((months % 2) != 0);
    MonthWidget *w;

    for (unsigned int i=0; i<MAX_MONTHS; i++) {
        w = new MonthWidget( interface, monthBox, i+1);
        if (i < urlList.count())
            w->setImage(urlList[i]);
        if (i<months)
            monthBoxLayout_->addWidget(w, i / inRow, i % inRow);
        else
            w->hide();
        mwVector_->insert(i, w);
    }

    TQLabel* tLabel =
        new TQLabel(i18n("Left click on Months to Select Images. "
                        "Right click to Clear Month.\n"
                        "You can also drag and drop images onto the Months"),
                                monthBox);

    monthBoxLayout_->addMultiCellWidget(tLabel, 2, 2, 0, 5);

    mainLayout->addWidget(monthBox);

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

    mainLayout->addItem(new TQSpacerItem(5,5,TQSizePolicy::Minimum,
                                        TQSizePolicy::Expanding));
}

void CalSelect::slotYearChanged(int year)
{
    int i, months;
    TQDate d, oldD;
    TDEGlobal::locale()->calendar()->setYMD(d, year, 1, 1);
    TDEGlobal::locale()->calendar()->setYMD(oldD, CalSettings::instance()->getYear(), 1, 1);
    months = TDEGlobal::locale()->calendar()->monthsInYear(d);

    if ((TDEGlobal::locale()->calendar()->monthsInYear(oldD) != months) && !mwVector_->isEmpty())
    {
        // hide the last months that are not present on the current year
        for (i=months; (i<TDEGlobal::locale()->calendar()->monthsInYear(oldD)) && (i<(int)mwVector_->count()); i++)
            mwVector_->at(i)->hide();

        // span the monthWidgets over 2 rows. inRow should usually be 6 or 7 (for 12 or 13 months)
        int inRow = (months / 2) + ((months % 2) != 0);
        // remove all the monthWidgets, then readd the needed ones
        for (i=0; i<TDEGlobal::locale()->calendar()->monthsInYear(oldD); i++) {
            monthBoxLayout_->remove(mwVector_->at(i));
        }
        for (i=0; (i<months) && (i<(int)mwVector_->count()); i++) {
            monthBoxLayout_->addWidget(mwVector_->at(i), i / inRow, i % inRow);
            if (mwVector_->at(i)->isHidden())
                mwVector_->at(i)->show();
            mwVector_->at(i)->update();
        }
    }
    CalSettings::instance()->setYear(year);
}

}  // NameSpace KIPICalendarPlugin

#include "calselect.moc"