summaryrefslogtreecommitdiffstats
path: root/digikam/digikam/tagspopupmenu.cpp
blob: bb0e05252f4090050064dc3257f36454927af819 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2004-09-07
 * Description : a pop-up menu implementation to display a 
 *               hierarchical view of digiKam tags.
 * 
 * Copyright (C) 2004 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot 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, 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.
 * 
 * ============================================================ */

#define ADDTAGID 10000

// TQt includes.

#include <tqpixmap.h>
#include <tqstring.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqvaluelist.h>
#include <tqpixmap.h>
#include <tqvaluevector.h>

// KDE includes.

#include <tdeapplication.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>

// Local includes.

#include "ddebug.h"
#include "albumiconview.h"
#include "albumiconitem.h"
#include "albummanager.h"
#include "albumdb.h"
#include "album.h"
#include "syncjob.h"
#include "tageditdlg.h"
#include "tagspopupmenu.h"
#include "tagspopupmenu.moc"

namespace Digikam
{

class TagsPopupCheckedMenuItem : public TQCustomMenuItem
{
public:

    TagsPopupCheckedMenuItem(TQPopupMenu* popup, const TQString& txt, const TQPixmap& pix)
        : TQCustomMenuItem(), m_popup(popup), m_txt(txt), m_pix(pix)
    {
    }

    virtual TQSize sizeHint()
    {
        TQFont fn = m_popup->font();
        TQFontMetrics fm(fn);
        int w = fm.width(m_txt) + 5 + kapp->style().pixelMetric(TQStyle::PM_IndicatorWidth, 0);
        int h = TQMAX(fm.height(), m_pix.height());
        return TQSize( w, h );
    }

    virtual void paint(TQPainter* p, const TQColorGroup& cg, bool act, bool enabled,
                       int x, int y, int w, int h )
    {
        p->save();
        p->setPen(act ? cg.highlightedText() : cg.highlight());
        p->drawText(x, y, w, h, TQt::AlignLeft|TQt::AlignVCenter, m_txt);
        p->restore();

        if (!m_pix.isNull())
        {
            TQRect pixRect(x/2 - m_pix.width()/2, y, m_pix.width(), m_pix.height());
            p->drawPixmap( pixRect.topLeft(), m_pix );
        }

        int checkWidth  = kapp->style().pixelMetric(TQStyle::PM_IndicatorWidth,  0);
        int checkHeight = kapp->style().pixelMetric(TQStyle::PM_IndicatorHeight, 0);

        TQStyle::SFlags flags = TQStyle::Style_Default;
        flags |= TQStyle::Style_On;
        if (enabled)
            flags |= TQStyle::Style_Enabled;
        if (act)
            flags |= TQStyle::Style_Active;
        
        TQFont fn = m_popup->font();
        TQFontMetrics fm(fn);
        TQRect r(x + 5 + fm.width(m_txt), y + (h/2-checkHeight/2), checkWidth, checkHeight);
        kapp->style().tqdrawPrimitive(TQStyle::PE_CheckMark, p, r, cg, flags);
    }

private:

    TQPopupMenu *m_popup;

    TQString     m_txt;

    TQPixmap     m_pix;
};

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

class TagsPopupMenuPriv
{
public:

    TagsPopupMenuPriv(){}

    int                 addToID;

    TQPixmap             addTagPix;

    TQValueList<int>     assignedTags;
    TQValueList<TQ_LLONG> selectedImageIDs;

    TagsPopupMenu::Mode mode;
};

TagsPopupMenu::TagsPopupMenu(const TQValueList<TQ_LLONG>& selectedImageIDs, int addToID, Mode mode)
             : TQPopupMenu(0)
{
    d = new TagsPopupMenuPriv;
    d->selectedImageIDs = selectedImageIDs;
    d->addToID          = addToID;
    d->mode             = mode;

    TDEIconLoader *iconLoader = TDEApplication::kApplication()->iconLoader();
    d->addTagPix            = iconLoader->loadIcon("tag",
                                        TDEIcon::NoGroup,
                                        TDEIcon::SizeSmall,
                                        TDEIcon::DefaultState,
                                        0, true);
    
    connect(this, TQT_SIGNAL(aboutToShow()),
            this, TQT_SLOT(slotAboutToShow()));

    connect(this, TQT_SIGNAL(activated(int)),
            this, TQT_SLOT(slotActivated(int)));
}

TagsPopupMenu::~TagsPopupMenu()
{
    delete d;
}

void TagsPopupMenu::clearPopup()
{
    d->assignedTags.clear();
    clear();
}

TQPopupMenu* TagsPopupMenu::buildSubMenu(int tagid)
{
    AlbumManager* man = AlbumManager::instance();
    TAlbum* album     = man->findTAlbum(tagid);
    if (!album)
        return 0;

    TQPopupMenu* popup = new TQPopupMenu(this);

    connect(popup, TQT_SIGNAL(activated(int)), 
            this, TQT_SLOT(slotActivated(int)));

    if (d->mode == ASSIGN)
    {
        popup->insertItem(d->addTagPix, i18n("Add New Tag..."), ADDTAGID + album->id());
        popup->insertSeparator();
                
        TQPixmap pix = SyncJob::getTagThumbnail(album);
        if ((d->mode == ASSIGN) && (d->assignedTags.contains(album->id())))
        {
            popup->insertItem(new TagsPopupCheckedMenuItem(popup, album->title(), pix),
                              d->addToID + album->id());
        }
        else
        {
            popup->insertItem(pix, album->title(), d->addToID + album->id());
        }                
        
        if (album->firstChild())
        {
            popup->insertSeparator();
        }
    }
    else
    {
        if (!album->isRoot())
        {
            TQPixmap pix = SyncJob::getTagThumbnail(album);
            popup->insertItem(pix, album->title(), d->addToID + album->id());
            popup->insertSeparator();
        }
    }
    
    iterateAndBuildMenu(popup, album);

    return popup;
}

void TagsPopupMenu::slotAboutToShow()
{
    clearPopup();

    AlbumManager* man = AlbumManager::instance();

    if (d->mode == REMOVE || d->mode == DISPLAY)
    {
        if (d->selectedImageIDs.isEmpty())
            return;

        d->assignedTags = man->albumDB()->getItemCommonTagIDs(d->selectedImageIDs);

        if (d->assignedTags.isEmpty())
            return;

        // also add the parents of the assigned tags
        IntList tList;
        for (IntList::iterator it = d->assignedTags.begin();
             it != d->assignedTags.end(); ++it)
        {
            TAlbum* album = man->findTAlbum(*it);
            if (album)
            {
                Album* a = album->parent();
                while (a)
                {
                    tList.append(a->id());
                    a = a->parent();
                }
            }
        }

        for (IntList::iterator it = tList.begin();
             it != tList.end(); ++it)
        {
            d->assignedTags.append(*it);
        }
    }
    else if (d->mode == ASSIGN)
    {
        if (d->selectedImageIDs.count() == 1)
        {
            d->assignedTags = man->albumDB()->getItemCommonTagIDs(d->selectedImageIDs);
        }
    }
        
    TAlbum* album = man->findTAlbum(0);
    if (!album)
        return;

    if (d->mode == ASSIGN)
    {
        insertItem(d->addTagPix, i18n("Add New Tag..."), ADDTAGID);
        if (album->firstChild())
        {
            insertSeparator();
        }
    }
    
    iterateAndBuildMenu(this, album);
}

// for qHeapSort
typedef TQPair<TQString, Album*> TagsMenuSortType;
bool operator<(const TagsMenuSortType &lhs, const TagsMenuSortType &rhs)
{
    return lhs.first < rhs.first;
}

void TagsPopupMenu::iterateAndBuildMenu(TQPopupMenu *menu, TAlbum *album)
{
    TQValueVector<TagsMenuSortType> sortedTags;

    for (Album* a = album->firstChild(); a; a = a->next())
    {
        sortedTags.push_back(tqMakePair(a->title(), a));
    }

    qHeapSort(sortedTags);
    
    for (TQValueVector<TagsMenuSortType>::Iterator i = sortedTags.begin(); i != sortedTags.end(); ++i)
    {
        Album *a = i->second;
        
        if (d->mode == REMOVE || d->mode == DISPLAY)
        {
            IntList::iterator it = tqFind(d->assignedTags.begin(), 
                                         d->assignedTags.end(), a->id());
            if (it == d->assignedTags.end())
                continue;
        }
        
        TQPixmap pix = SyncJob::getTagThumbnail((TAlbum*)a);
        TQString t = a->title();
        t.replace('&',"&&");

        if (a->firstChild())
        {
            menu->insertItem(pix, t, buildSubMenu(a->id()));
        }
        else
        {
            if ((d->mode == ASSIGN) && (d->assignedTags.contains(a->id())))
            {
                menu->insertItem(new TagsPopupCheckedMenuItem(this, a->title(), pix),
                                                              d->addToID + a->id());
            }
            else
            {
                menu->insertItem(pix, t, d->addToID + a->id());
            }
        }
    }
}

void TagsPopupMenu::slotActivated(int id)
{
    if (id >= ADDTAGID)
    {
        int tagID = id - ADDTAGID;

        AlbumManager* man = AlbumManager::instance();
        TAlbum* parent    = man->findTAlbum(tagID);
        if (!parent)
        {
            DWarning() << "Failed to find album with id "
                       << tagID << endl;
            return;
        }

        TQString title, icon;
        if (!TagEditDlg::tagCreate(TQT_TQWIDGET(kapp->activeWindow()), parent, title, icon))
            return;

        TQMap<TQString, TQString> errMap;
        AlbumList tList = TagEditDlg::createTAlbum(parent, title, icon, errMap);
        TagEditDlg::showtagsListCreationError(TQT_TQWIDGET(kapp->activeWindow()), errMap);

        for (AlbumList::iterator it = tList.begin(); it != tList.end(); ++it)
            emit signalTagActivated((*it)->id());
    }
    else
    {
        int tagID = id - d->addToID;
        emit signalTagActivated(tagID);
    }
}

}  // namespace Digikam