summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/calendar/monthwidget.cpp
blob: 31929bbfac552d57ca195ceacf60444e9531223a (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
/* ============================================================
 * File  : monthwidget.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 <tqdatetime.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqevent.h>
#include <tqdragobject.h>
#include <tqstrlist.h>
#include <tqimage.h>

// KDE includes.

#include <kurl.h>
#include <kurldrag.h>
#include <kiconloader.h>
#include <tdefiledialog.h>
#include <kimageio.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdeapplication.h>
#include <kiconloader.h>
#include <kdebug.h>
#include <tdeio/previewjob.h>
#include <tdeversion.h>
#include <kcalendarsystem.h>

// LibKipi includes.

#include <libkipi/imagedialog.h>

// Local includes.

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

namespace KIPICalendarPlugin
{

MonthWidget::MonthWidget( KIPI::Interface* interface, TQWidget *parent, int month)
    : TQFrame(parent), interface_( interface )
{
    setAcceptDrops(true);
    month_     = month;
    imagePath_ = TQString("");
    pixmap_    = new TQPixmap(SmallIcon("file_broken",
                                       TDEIcon::SizeMedium,
                                       TDEIcon::DisabledState));
    setFixedSize(TQSize(70,90));
    setFrameStyle(TQFrame::Panel|TQFrame::Raised);
}

MonthWidget::~MonthWidget()
{
    if (pixmap_) delete pixmap_;
}

KURL MonthWidget::imagePath()
{
    return imagePath_;
}

void MonthWidget::drawContents(TQPainter *p)
{

#if KDE_IS_VERSION(3,2,0)
    TQString name = TDEGlobal::locale()->calendar()->monthName(month_, CalSettings::instance()->getYear(), true);
#else
    TQString name = TDEGlobal::locale()->monthName(month_, true);
#endif

    TQRect cr;

    cr = contentsRect();
    cr.setBottom(70);
    p->drawPixmap(cr.width()/2 - pixmap_->width()/2,
                  cr.height()/2 - pixmap_->height()/2,
                  *pixmap_);

    cr = contentsRect();
    cr.setTop(70);
    p->drawText(cr,TQt::AlignHCenter,name);
}

void MonthWidget::dragEnterEvent(TQDragEnterEvent* event)
{
    event->accept(TQUriDrag::canDecode(event));
}

void MonthWidget::setImage( const KURL &url )
{
    if (!url.isValid())
        return;

    // check if the file is an image
    if ( ! TQImageIO::imageFormat( url.path() ) )
    {
        kdWarning( 51001 ) << "Unknown image format for: "
                << url.prettyURL() << endl;
        return;
    }

    imagePath_ = url;
    CalSettings::instance()->setImage(month_, imagePath_);

    TDEIconLoader* iconLoader = TDEApplication::kApplication()->iconLoader();
    TQPixmap pix = iconLoader->loadIcon("image", TDEIcon::NoGroup, 64 );
    if ( pixmap_ )
        delete pixmap_;
    pixmap_ = new TQPixmap( pix );
    update();

    KURL::List urls;
    urls << url;

    TDEIO::PreviewJob* thumbJob_ =
            TDEIO::filePreview( urls,64);
    connect(thumbJob_, TQT_SIGNAL(gotPreview(const KFileItem*, const TQPixmap&)),
            TQT_SLOT(slotGotThumbnaiL(const KFileItem*, const TQPixmap&)));
}

void MonthWidget::dropEvent(TQDropEvent* event)
{
    KURL::List srcURLs;
    if ( !KURLDrag::decode(event, srcURLs) )
        return;

    if ( srcURLs.isEmpty() )
        return;

    KURL url = srcURLs.first();
    setImage( url );
}

void MonthWidget::slotGotThumbnaiL(const KFileItem* , const TQPixmap& pix)
{
    if ( pixmap_ )
        delete pixmap_;
    TQPixmap image = pix;
    int angle = interface_->info( imagePath_ ).angle();
    if ( angle != 0 ) {
        TQWMatrix matrix;
        matrix.rotate( angle );
        image = image.xForm( matrix );
    }

    pixmap_ = new TQPixmap(image);
    update();
}

void MonthWidget::mouseReleaseEvent(TQMouseEvent* e)
{
    if (!contentsRect().contains(e->pos())) return;

    if (e->button() == Qt::LeftButton)
    {
        KURL url = KIPI::ImageDialog::getImageURL(this, interface_);
        setImage(url);
    }
    else if (e->button() == Qt::RightButton) {
        imagePath_ = TQString("");
        CalSettings::instance()->setImage(month_,imagePath_);
        delete pixmap_;
        pixmap_    = new TQPixmap(SmallIcon("file_broken",
                                           TDEIcon::SizeMedium,
                                           TDEIcon::DisabledState));
        update();
    }
}

}  // NameSpace KIPICalendarPlugin

#include "monthwidget.moc"