~ubuntu-branches/ubuntu/trusty/kvirc/trusty

« back to all changes in this revision

Viewing changes to src/kvirc/ui/KviImageDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kai Wasserbäch, Kai Wasserbäch, Raúl Sánchez Siles
  • Date: 2011-02-12 10:40:21 UTC
  • mfrom: (14.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212104021-5mh4f75jlku20mnt
The combined "Twisted Experiment" and "Nocturnal Raid" release.

[ Kai Wasserbäch ]
* Synced to upstream's SVN revision 5467.
* debian/rules:
  - Added .PHONY line.
  - Resurrect -DMANUAL_REVISION, got lost somewhere and we build SVN
    revisions again.
  - Replace "-DWITH_NO_EMBEDDED_CODE=YES" with "-DWANT_CRYPTOPP=YES".
  - Change the remaining -DWITH/-DWITHOUT to the new -DWANT syntax.
* debian/control:
  - Removed DMUA, I'm a DD now.
  - Changed my e-mail address.
  - Removed unneeded relationships (no upgrades over two releases are
    supported).
  - Fix Suggests for kvirc-dbg.
  - kvirc-data: Make the "Suggests: kvirc" a Recommends, doesn't make much
    sense to install the -data package without the program.
* debian/source/local-options: Added with "unapply-patches".
* debian/kvirc.lintian-overrides: Updated to work for 4.1.1.
* debian/patches/21_make_shared-mime-info_B-D_superfluous.patch: Updated.
* debian/kvirc-data.install: Added .notifyrc.

[ Raúl Sánchez Siles ]
* Stating the right version where kvirc-data break and replace should happen.
* Fixing link to license file.
* Added French and Portuguese man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//
 
3
//   File : KviImageDialog.cpp
 
4
//   Creation date : Sun Dec 22 2002 19:42 by Szymon Stefanek
 
5
//
 
6
//   This file is part of the KVIrc irc client distribution
 
7
//   Copyright (C) 2002-2010 Szymon Stefanek (pragma at kvirc dot net)
 
8
//
 
9
//   This program is FREE software. You can redistribute it and/or
 
10
//   modify it under the terms of the GNU General Public License
 
11
//   as published by the Free Software Foundation; either version 2
 
12
//   of the License, or (at your opinion) any later version.
 
13
//
 
14
//   This program is distributed in the HOPE that it will be USEFUL,
 
15
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
17
//   See the GNU General Public License for more details.
 
18
//
 
19
//   You should have received a copy of the GNU General Public License
 
20
//   along with this program. If not, write to the Free Software Foundation,
 
21
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
//
 
23
//=============================================================================
 
24
 
 
25
#include "KviImageDialog.h"
 
26
#include "KviFileUtils.h"
 
27
#include "KviLocale.h"
 
28
#include "KviIconManager.h"
 
29
#include "KviOptions.h"
 
30
#include "KviApplication.h"
 
31
 
 
32
#include <QLayout>
 
33
#include <QPushButton>
 
34
#include <QPainter>
 
35
#include <QImage>
 
36
#include <QFileInfo>
 
37
#include <QDir>
 
38
 
 
39
 
 
40
int KviImageDialogItem::height(const KviTalListWidget *lb) const
 
41
{
 
42
        return pixmap()->height() + 12 + lb->fontMetrics().lineSpacing();
 
43
}
 
44
 
 
45
int KviImageDialogItem::width(const KviTalListWidget *lb) const
 
46
{
 
47
        int w;
 
48
        if(text().isEmpty())w = 24;
 
49
        w = lb->fontMetrics().width(text()) + 4;
 
50
        if(w > 100)w = 100;
 
51
        if(w < 24)w = 24;
 
52
        return qMax(pixmap()->width() + 10,w);
 
53
}
 
54
 
 
55
void KviImageDialogItem::paint(QPainter * p)
 
56
{
 
57
        const QPixmap *pm = pixmap();
 
58
        if(pm && !pm->isNull())p->drawPixmap(5,5, *pm);
 
59
 
 
60
        if(!m_bIsFolder)
 
61
        {
 
62
                p->setPen(Qt::gray);
 
63
                p->drawRect(3,3,pm->width() + 4,pm->height() + 4);
 
64
        }
 
65
 
 
66
        QRect daRect(listWidget()->visualItemRect(this));
 
67
 
 
68
        p->setPen(Qt::black);
 
69
        p->drawRect(1,1,daRect.width() - 2,daRect.height() - 2);
 
70
 
 
71
        if(text().isEmpty())return;
 
72
 
 
73
        QString t = text();
 
74
 
 
75
        QFontMetrics fm(p->fontMetrics());
 
76
 
 
77
        int wdth = fm.width(t);
 
78
 
 
79
        int idx = t.length();
 
80
        while(wdth > (daRect.width() - 6) && idx > 3)
 
81
        {
 
82
                t = text();
 
83
                t.truncate(idx);
 
84
                t.append("...");
 
85
                wdth = fm.width(t);
 
86
                idx--;
 
87
        }
 
88
 
 
89
        p->drawText(3,pm->height() + 8,daRect.width() - 6,daRect.height() - (pm->height() + 6),Qt::AlignLeft | Qt::AlignTop,t);
 
90
}
 
91
 
 
92
 
 
93
KviImageDialog::KviImageDialog(QWidget * par,
 
94
                const QString &szCaption,
 
95
                int types,
 
96
                int initialType,
 
97
                const QString &szInitialDir,
 
98
                int maxPreviewFileSize,
 
99
                bool modal
 
100
        )
 
101
        : QDialog(par)
 
102
{
 
103
        m_szInitialPath = szInitialDir;
 
104
        setModal(modal);
 
105
        m_iMaxPreviewFileSize = maxPreviewFileSize;
 
106
 
 
107
        setWindowTitle(szCaption.isEmpty() ? __tr2qs("Choose image ...") : szCaption);
 
108
 
 
109
        m_pTimer = new QTimer(this);
 
110
        connect(m_pTimer,SIGNAL(timeout()),this,SLOT(heartbeat()));
 
111
 
 
112
        QGridLayout * g = new QGridLayout(this);
 
113
 
 
114
        m_pTypeComboBox = new QComboBox(this);
 
115
 
 
116
        g->addWidget(m_pTypeComboBox,0,0,1,3);
 
117
 
 
118
        m_pTypeList = new QList<int>;
 
119
 
 
120
        QString bi = __tr2qs("Builtin images");
 
121
 
 
122
        QString tmp = bi;
 
123
 
 
124
        if((types & KID_TYPE_ALL) == 0)types = KID_TYPE_FULL_PATH;
 
125
 
 
126
        if(types & KID_TYPE_BUILTIN_IMAGES_SMALL)
 
127
        {
 
128
                tmp += ": ";
 
129
                tmp += __tr2qs("Small icons");
 
130
                m_pTypeComboBox->insertItem(0,tmp);
 
131
                m_pTypeList->append(KID_TYPE_BUILTIN_IMAGES_SMALL);
 
132
        }
 
133
 
 
134
        if(types & KID_TYPE_FULL_PATH)
 
135
        {
 
136
                m_pTypeComboBox->insertItem(m_pTypeComboBox->count(),__tr2qs("Full path"));
 
137
                m_pTypeList->append(KID_TYPE_FULL_PATH);
 
138
        }
 
139
 
 
140
        int idx = m_pTypeList->indexOf(initialType);
 
141
        if(idx < 0)idx = 0;
 
142
 
 
143
        QWidget * l = new QWidget(this);
 
144
        g->addWidget(l,1,0,1,3);
 
145
 
 
146
 
 
147
        m_pListBox = new KviTalListWidget(this);
 
148
        //not supported by listwidget.. we'd need treewidget here
 
149
//      m_pListBox->setColumnMode(KviTalListWidget::FitToWidth);
 
150
//      m_pListBox->setRowMode(KviTalListWidget::Variable);
 
151
 
 
152
        m_pTip = new KviDynamicToolTip(m_pListBox->viewport());
 
153
 
 
154
        g->addWidget(m_pListBox,2,0,1,3);
 
155
 
 
156
        QPushButton * b = new QPushButton(__tr2qs("Cancel"),this);
 
157
        connect(b,SIGNAL(clicked()),this,SLOT(cancelClicked()));
 
158
        g->addWidget(b,3,1);
 
159
 
 
160
        b = new QPushButton(__tr2qs("Ok"),this);
 
161
        connect(b,SIGNAL(clicked()),this,SLOT(okClicked()));
 
162
        g->addWidget(b,3,2);
 
163
 
 
164
        g->setRowStretch(2,1);
 
165
        g->setColumnStretch(0,1);
 
166
 
 
167
        connect(m_pTypeComboBox,SIGNAL(activated(int)),this,SLOT(jobTypeSelected(int)));
 
168
        connect(m_pListBox,SIGNAL(itemActivated(QListWidgetItem *)),this,SLOT(itemDoubleClicked(QListWidgetItem *)));
 
169
        connect(m_pTip,SIGNAL(tipRequest(KviDynamicToolTip *,const QPoint &)),this,SLOT(tipRequest(KviDynamicToolTip *,const QPoint &)));
 
170
 
 
171
        m_pTypeComboBox->setCurrentIndex(idx);
 
172
        jobTypeSelected(idx);
 
173
 
 
174
        m_pListBox->setMinimumSize(420,350);
 
175
}
 
176
 
 
177
KviImageDialog::~KviImageDialog()
 
178
{
 
179
        delete m_pTimer;
 
180
        delete m_pTypeList;
 
181
}
 
182
 
 
183
void KviImageDialog::jobTypeSelected(int index)
 
184
{
 
185
        if(index < 0)return;
 
186
        if(index >= (int)(m_pTypeList->count()))index = (int)m_pTypeList->count();
 
187
        if(m_szInitialPath.isEmpty())
 
188
                startJob((m_pTypeList->at(index)),KVI_OPTION_STRING(KviOption_stringLastImageDialogPath));
 
189
        else {
 
190
                startJob((m_pTypeList->at(index)),m_szInitialPath);
 
191
                m_szInitialPath = ""; // clear it so we will use the last path
 
192
        }
 
193
}
 
194
 
 
195
void KviImageDialog::startJob(int type,const QString &szInitialPath)
 
196
{
 
197
        m_pTimer->stop();
 
198
        m_iJobType = type;
 
199
 
 
200
        m_iJobIndexHelper = 0;
 
201
        if(m_iJobType == KID_TYPE_FULL_PATH)
 
202
        {
 
203
                QDir d(szInitialPath);
 
204
                if(!d.exists())d = QDir::homePath();
 
205
                if(!d.exists())d = QDir::rootPath();
 
206
                m_szJobPath = d.absolutePath();
 
207
                KVI_OPTION_STRING(KviOption_stringLastImageDialogPath) = m_szJobPath;
 
208
                m_lJobFileList = d.entryList(QDir::Hidden | QDir::AllEntries,QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
 
209
        }
 
210
 
 
211
        m_pTimer->start(100);
 
212
}
 
213
 
 
214
void KviImageDialog::jobTerminated()
 
215
{
 
216
        m_pTimer->stop();
 
217
}
 
218
 
 
219
void KviImageDialog::heartbeat()
 
220
{
 
221
        if(m_iJobIndexHelper == 0)
 
222
                m_pListBox->clear();
 
223
 
 
224
        switch(m_iJobType)
 
225
        {
 
226
                case KID_TYPE_BUILTIN_IMAGES_SMALL:
 
227
                {
 
228
                        if(m_iJobIndexHelper >= KviIconManager::IconCount)
 
229
                        {
 
230
                                jobTerminated();
 
231
                                return;
 
232
                        }
 
233
                        int max = m_iJobIndexHelper + 15;
 
234
                        if(max > KviIconManager::IconCount)
 
235
                                max = KviIconManager::IconCount;
 
236
                        while(m_iJobIndexHelper < max)
 
237
                        {
 
238
                                QString szId = g_pIconManager->getSmallIconName(m_iJobIndexHelper);
 
239
                                QString szTip;
 
240
                                szTip = QString(__tr2qs("Builtin $icon(%1) [index %2]")).arg(szId).arg(m_iJobIndexHelper);
 
241
                                QString szImageId = "$icon(";
 
242
                                szImageId += szId;
 
243
                                szImageId += ")";
 
244
                                new KviImageDialogItem(m_pListBox,*(g_pIconManager->getSmallIcon(m_iJobIndexHelper)),szId,szImageId,szTip);
 
245
                                m_iJobIndexHelper++;
 
246
                        }
 
247
                }
 
248
                break;
 
249
                case KID_TYPE_FULL_PATH:
 
250
                {
 
251
                        m_iJobIndexHelper++;
 
252
                        if(m_lJobFileList.isEmpty())
 
253
                        {
 
254
                                jobTerminated();
 
255
                                return;
 
256
                        }
 
257
                        int idx = 0;
 
258
                        while((idx < 20) && (!m_lJobFileList.isEmpty()))
 
259
                        {
 
260
                                QString szFile = m_lJobFileList.first();
 
261
                                m_lJobFileList.removeAll(szFile);
 
262
                                QString szPath = m_szJobPath;
 
263
                                szPath += KVI_PATH_SEPARATOR;
 
264
                                szPath += szFile;
 
265
                                QFileInfo fi(szPath);
 
266
                                idx += fi.size() / 128000; // we do less entries when have big files to read
 
267
                                if(fi.isDir())
 
268
                                {
 
269
                                        if(szFile != ".")
 
270
                                        {
 
271
                                                QString tip = szFile;
 
272
                                                tip += "<br><hr>";
 
273
                                                tip += __tr2qs("directory");
 
274
                                                new KviImageDialogItem(m_pListBox,*(g_pIconManager->getBigIcon(KVI_BIGICON_FOLDER)),szFile,szPath,tip,true);
 
275
                                        }
 
276
                                } else {
 
277
                                        if(((int)fi.size()) < m_iMaxPreviewFileSize)
 
278
                                        {
 
279
                                                QImage i(szPath);
 
280
                                                if(i.isNull())continue;
 
281
                                                QPixmap pix;
 
282
                                                if((i.width() > 80) || (i.height() > 80))pix = QPixmap::fromImage(i.scaled(80,80,Qt::KeepAspectRatio));
 
283
                                                else pix = QPixmap::fromImage(i);
 
284
 
 
285
                                                QString tip = szFile;
 
286
                                                tip += "<br><hr>";
 
287
                                                QString sz;
 
288
                                                sz.setNum(i.width());
 
289
                                                tip += sz;
 
290
                                                tip += " x ";
 
291
                                                sz.setNum(i.height());
 
292
                                                tip += sz;
 
293
                                                tip += " ";
 
294
                                                tip += __tr2qs("pixels");
 
295
                                                tip += "<br>";
 
296
                                                sz.setNum(fi.size());
 
297
                                                tip += sz;
 
298
                                                tip += " ";
 
299
                                                tip += __tr2qs("bytes");
 
300
                                                tip += "<br>";
 
301
 
 
302
                                                new KviImageDialogItem(m_pListBox,pix,szFile,szPath,tip);
 
303
                                        }
 
304
                                }
 
305
                                idx++;
 
306
                        }
 
307
                }
 
308
                break;
 
309
        }
 
310
}
 
311
 
 
312
void KviImageDialog::okClicked()
 
313
{
 
314
        KviTalListWidgetItem * it = (KviTalListWidgetItem*) m_pListBox->currentItem();
 
315
        if(!it)return;
 
316
        itemDoubleClicked(it);
 
317
}
 
318
 
 
319
void KviImageDialog::cancelClicked()
 
320
{
 
321
        m_szSelectedImage = QString();
 
322
        reject();
 
323
}
 
324
 
 
325
void KviImageDialog::closeEvent(QCloseEvent * e)
 
326
{
 
327
        m_szSelectedImage = QString();
 
328
        QDialog::closeEvent(e);
 
329
}
 
330
 
 
331
void KviImageDialog::itemDoubleClicked(QListWidgetItem * it)
 
332
{
 
333
        if(!it)return;
 
334
        KviImageDialogItem * i = (KviImageDialogItem *)it;
 
335
        if(i->isFolder())
 
336
        {
 
337
                startJob(KID_TYPE_FULL_PATH,i->imageId());
 
338
        } else {
 
339
                QString szImageId = i->imageId();
 
340
                if(szImageId.length() > 0)
 
341
                {
 
342
                        if(szImageId.at(0) == QChar('$'))
 
343
                                m_szSelectedImage = szImageId; // it's $icon(something)
 
344
                        else
 
345
                                g_pApp->mapImageFile(m_szSelectedImage,i->imageId()); // it's a file and we need to map it to our filesystem view
 
346
                        accept();
 
347
                }
 
348
        }
 
349
}
 
350
 
 
351
void KviImageDialog::tipRequest(KviDynamicToolTip *,const QPoint &pnt)
 
352
{
 
353
        KviTalListWidgetItem * it = (KviTalListWidgetItem *)m_pListBox->itemAt(pnt);
 
354
        if(!it)return;
 
355
        QRect r = m_pListBox->visualItemRect(it);
 
356
        KviImageDialogItem * i = (KviImageDialogItem *)it;
 
357
        m_pTip->tip(r,i->tipText());
 
358
}