summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_dragprovider.cpp
blob: 69ae6b32a0edfdf1ca8713fc976f2eccfac38bb1 (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
/***************************************************************************
                          sq_dragprovider.cpp  -  description
                             -------------------
    begin                : ??? Sep 17 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqwidget.h>
#include <tqdragobject.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqfontmetrics.h>
#include <tqtooltip.h>
#include <tqvaluevector.h>
#include <tqsize.h>

#include <kstringhandler.h>
#include <kurldrag.h>
#include <klocale.h>
#include <kurl.h>

#include <algorithm>

#include "sq_dragprovider.h"
#include "sq_iconloader.h"
#include "sq_filethumbviewitem.h"
#include "sq_thumbnailsize.h"

#define SQ_THUMB_SIZE 96

SQ_DragProvider * SQ_DragProvider::m_inst = 0;

SQ_DragProvider::SQ_DragProvider(TQObject *tqparent) : TQObject(tqparent), source(0)
{
    m_inst = this;
}

SQ_DragProvider::~SQ_DragProvider()
{}

void SQ_DragProvider::setParams(TQWidget *_source, const KFileItemList &_files, SourceType tp)
{
    source = _source;
    files = _files;
    type = tp;
}

void SQ_DragProvider::start()
{
    if(!source)
        return;

    int count = files.count();

    if(!count)
        return;

    KURL::List urls;
    KFileItem *fi;
    SQ_FileThumbViewItem *kfi;
    TQStringList names;
    int capas = type == Thumbnails ? 2 : 10; // 2 thumbnails or 10 file names
    int _capas = capas;
    TQValueVector<TQPixmap> pixmaps;
    TQPixmap *pix;
    int pixw = 0, pixh = 0;
    const int margin = 2;

    KFileItem *first = files.first();

    KFileItemListIterator it(files);

    while((fi = it.current()))
    {
        urls.append(fi->url());

        if(_capas)
        {
            if(type == Thumbnails)
            {
                kfi = reinterpret_cast<SQ_FileThumbViewItem *>(fi->extraData(source));

                if(kfi && (pix = kfi->pixmap()))
                {
                    pixmaps.append(*pix);

                    if(!pixw || !pixh)
                    {
                        pixw = pix->width();
                        pixh = pix->height();

                        if(pixw > SQ_THUMB_SIZE)
                        {
                            TQSize sz(pixw, pixh);
                            sz.tqscale(SQ_THUMB_SIZE, SQ_THUMB_SIZE, TQSize::ScaleMin);
                            pixw = sz.width();
                            pixh = sz.height();
                        }
                    }
                }
            }
            else
                names.append(KStringHandler::csqueeze(fi->name()));

            --_capas;
        }

        ++it;
    }

    TQDragObject *drag = new KURLDrag(urls, source);
    TQPixmap dragIcon;

    if(urls.count() > 1)
        dragIcon = SQ_IconLoader::instance()->loadIcon("kmultiple", KIcon::Desktop, KIcon::SizeSmall);
    else
        dragIcon = first->pixmap(KIcon::SizeSmall);

    const int flags = TQt::AlignAuto|TQt::AlignVCenter|TQt::ShowPrefix;
    TQPixmap dragPixmap;
    TQString text;

    // construct text
    if(type == Icons)
    {
        TQStringList::iterator sEnd = names.end();
        for(TQStringList::iterator sit = names.begin();sit != sEnd;++sit)
            text = text + *sit + '\n';
    }

    text += TQString::tqfromLatin1("%1 %2").tqarg(i18n("Total")).tqarg(i18n("1 file", "%n files", count));

    TQFontMetrics ms(source->font());
    TQRect r = ms.boundingRect(0, 0, 1, 1, flags, text);

    // resize pixmap
    if(type == Icons)
        dragPixmap.resize(r.width(), r.height());
    else
        dragPixmap.resize(std::max(r.width(), pixw)+margin*2, r.height()+margin+(pixh+margin)*pixmaps.count());

    TQPalette pal = TQToolTip::palette();
    TQPainter p;
    p.begin(&dragPixmap);

    // draw frame
    p.setPen(TQPen(pal.color(TQPalette::Active, TQColorGroup::Text)));
    p.setBrush(TQBrush(pal.color(TQPalette::Active, TQColorGroup::Background)));
    p.drawRect(dragPixmap.rect());

    // draw file names
    if(type == Icons)
        p.drawText(dragPixmap.rect(), flags, text);
    else // or thumbnails
    {
        int y = margin;
        TQValueVector<TQPixmap>::iterator itEnd = pixmaps.end();

        for(TQValueVector<TQPixmap>::iterator it = pixmaps.begin();it != itEnd;++it)
        {
            p.drawPixmap(TQRect((dragPixmap.width()-pixw)/2, y, pixw, pixh), *it);
            y = y + pixh + margin;
        }

        p.drawText(0, y, dragPixmap.width(), dragPixmap.height()-y, flags, text);
    }

    p.end();

    // finally, setup drag object
    drag->setPixmap(dragPixmap, TQPoint(16, -16));
    drag->dragCopy();
}