summaryrefslogtreecommitdiffstats
path: root/digikam/digikam/tageditdlg.cpp
blob: d77a016909ae28cfaae889fbdb95b9d5699d79a6 (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2004-07-01
 * Description : dialog to edit and create digiKam Tags
 * 
 * Copyright (C) 2004-2005 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.
 *
 * ============================================================ */

// TQt includes.

#include <tqlabel.h>
#include <tqlayout.h>

// KDE includes.

#include <tdelocale.h>
#include <kicondialog.h>
#include <tdeapplication.h>
#include <tdeversion.h>
#include <kiconloader.h>
#include <kseparator.h>
#include <tdelistview.h>

// Local includes.

#include "ddebug.h"
#include "album.h"
#include "searchtextbar.h"
#include "syncjob.h"
#include "tageditdlg.h"
#include "tageditdlg.moc"

namespace Digikam
{

class TagsListCreationErrorDialog : public KDialogBase
{

public:

    TagsListCreationErrorDialog(TQWidget* parent, const TQMap<TQString, TQString>& errMap);
    ~TagsListCreationErrorDialog(){};
};

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

class TagEditDlgPriv
{
public:

    TagEditDlgPriv()
    {
        titleEdit       = 0;
        iconButton      = 0;
        resetIconButton = 0;
        mainRootAlbum   = 0;
        topLabel        = 0;
        create          = false;
    }

    bool           create;

    TQLabel        *topLabel;

    TQString        icon;

    TQPushButton   *iconButton;
    TQPushButton   *resetIconButton;

    TAlbum        *mainRootAlbum;
    SearchTextBar *titleEdit;
};

TagEditDlg::TagEditDlg(TQWidget *parent, TAlbum* album, bool create)
          : KDialogBase(parent, 0, true, 0, Help|Ok|Cancel, Ok, true)
{
    d = new TagEditDlgPriv;
    d->mainRootAlbum = album; 
    d->create        = create;

    setHelp("tagscreation.anchor", "digikam");
    if (d->create) setCaption(i18n("New Tag"));
    else        setCaption(i18n("Edit Tag"));

    TQWidget *page     = makeMainWidget();
    TQGridLayout* grid = new TQGridLayout(page, 5, 4, 0, spacingHint());

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

    TQLabel *logo            = new TQLabel(page);
    TDEIconLoader* iconLoader = TDEApplication::kApplication()->iconLoader();
    logo->setPixmap(iconLoader->loadIcon("digikam", TDEIcon::NoGroup, 96, TDEIcon::DefaultState, 0, true));

    d->topLabel = new TQLabel(page);
    d->topLabel->setAlignment(TQt::AlignAuto | TQt::AlignVCenter | TQt::SingleLine);

    KSeparator *line = new KSeparator (Horizontal, page);

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

    TQLabel *titleLabel = new TQLabel(page);
    titleLabel->setText(i18n("&Title:"));

    d->titleEdit = new SearchTextBar(page, "TagEditDlgTitleEdit", i18n("Enter tag name here..."));
    titleLabel->setBuddy(d->titleEdit);

    TQLabel *tipLabel = new TQLabel(page);
    tipLabel->setTextFormat(TQt::RichText);
    tipLabel->setText(i18n("<qt><p>To create new tags, you can use the following rules:</p>"
                           "<p><ul><li>'/' can be used to create a tags hierarchy.<br>"
                           "Ex.: <i>\"Country/City/Paris\"</i></li>"
                           "<li>',' can be used to create more than one tags hierarchy at the same time.<br>"
                           "Ex.: <i>\"City/Paris, Monument/Notre-Dame\"</i></li>"
                           "<li>If a tag hierarchy starts with '/', root tag album is used as parent.</li></ul></p></qt>"
                           ));

    if (d->create) 
    {
        AlbumList tList = AlbumManager::instance()->allTAlbums();
        for (AlbumList::iterator it = tList.begin(); it != tList.end(); ++it)
        {
            TAlbum *tag = dynamic_cast<TAlbum*>(*it);
            d->titleEdit->lineEdit()->completionObject()->addItem(tag->tagPath());
        }
    }
    else
    {
        d->titleEdit->setText(d->mainRootAlbum->title());
        tipLabel->hide();
    }

    TQLabel *iconTextLabel = new TQLabel(page);
    iconTextLabel->setText(i18n("&Icon:"));

    d->iconButton = new TQPushButton(page);
    d->iconButton->setFixedSize(40, 40);
    iconTextLabel->setBuddy(d->iconButton);

    // In create mode, by default assign the icon of the parent (if not root) to this new tag.
    if (d->create && !d->mainRootAlbum->isRoot())
        d->icon = d->mainRootAlbum->icon();
    else
        d->icon = d->mainRootAlbum->icon();

    d->iconButton->setIconSet(SyncJob::getTagThumbnail(d->icon, 20));

    d->resetIconButton = new TQPushButton(SmallIcon("reload_page"), i18n("Reset"), page);
    if (d->create) d->resetIconButton->hide();

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

    grid->addMultiCellWidget(logo,               0, 3, 0, 0);
    grid->addMultiCellWidget(d->topLabel,        0, 0, 1, 4);
    grid->addMultiCellWidget(line,               1, 1, 1, 4);
    grid->addMultiCellWidget(tipLabel,           2, 2, 1, 4);
    grid->addMultiCellWidget(titleLabel,         3, 3, 1, 1);
    grid->addMultiCellWidget(d->titleEdit,       3, 3, 2, 4);
    grid->addMultiCellWidget(iconTextLabel,      4, 4, 1, 1);
    grid->addMultiCellWidget(d->iconButton,      4, 4, 2, 2);
    grid->addMultiCellWidget(d->resetIconButton, 4, 4, 3, 3);
    grid->setColStretch(4, 10);
    grid->setRowStretch(5, 10);

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

    connect(d->iconButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotIconChanged()));

    connect(d->resetIconButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotIconResetClicked()));

    connect(d->titleEdit->lineEdit(), TQ_SIGNAL(textChanged(const TQString&)),
            this, TQ_SLOT(slotTitleChanged(const TQString&)));

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

    slotTitleChanged(d->titleEdit->text());
    d->titleEdit->lineEdit()->setFocus();
    adjustSize();
}

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

TQString TagEditDlg::title() const
{
    return d->titleEdit->text();
}

TQString TagEditDlg::icon() const
{
    return d->icon;
}

void TagEditDlg::slotIconResetClicked()
{
    d->icon = TQString("tag");
    d->iconButton->setIconSet(SyncJob::getTagThumbnail(d->icon, 20));
}

void TagEditDlg::slotIconChanged()
{
#if KDE_IS_VERSION(3,3,0)
    TDEIconDialog dlg(this);
    dlg.setup(TDEIcon::NoGroup, TDEIcon::Application, false, 20, false, false, false);
    TQString icon = dlg.openDialog();
#else
    TQString icon = TDEIconDialog::getIcon(TDEIcon::NoGroup, TDEIcon::Application, false, 20);
    if (icon.startsWith("/"))
        return;
#endif

    if (icon.isEmpty() || icon == d->icon)
        return;

    d->icon = icon;
    d->iconButton->setIconSet(SyncJob::getTagThumbnail(d->icon, 20));
}

void TagEditDlg::slotTitleChanged(const TQString& newtitle)
{
    TQString tagName = d->mainRootAlbum->tagPath();
    if (tagName.endsWith("/") && !d->mainRootAlbum->isRoot()) 
        tagName.truncate(tagName.length()-1);

    if (d->create)
    {
        if (d->titleEdit->text().startsWith("/"))
        {
            d->topLabel->setText(i18n("<qt><b>Create New Tag</b></qt>"));
        }
        else
        {
            d->topLabel->setText(i18n("<qt><b>Create New Tag in<br>"
                                      "<i>\"%1\"</i></b></qt>").arg(tagName));
        }
    }
    else
    {
        d->topLabel->setText(i18n("<qt><b>Properties of Tag<br>"
                                  "<i>\"%1\"</i></b></qt>").arg(tagName));
    }

    enableButtonOK(!newtitle.isEmpty());
}

bool TagEditDlg::tagEdit(TQWidget *parent, TAlbum* album, TQString& title, TQString& icon)
{
    TagEditDlg dlg(parent, album);

    bool valRet = dlg.exec();
    if (valRet == TQDialog::Accepted)
    {
        title = dlg.title();
        icon  = dlg.icon();
    }

    return valRet;
}

bool TagEditDlg::tagCreate(TQWidget *parent, TAlbum* album, TQString& title, TQString& icon)
{
    TagEditDlg dlg(parent, album, true);

    bool valRet = dlg.exec();
    if (valRet == TQDialog::Accepted)
    {
        title = dlg.title();
        icon  = dlg.icon();
    }

    return valRet;
}

AlbumList TagEditDlg::createTAlbum(TAlbum *mainRootAlbum, const TQString& tagStr, const TQString& icon,
                                   TQMap<TQString, TQString>& errMap)
{
    errMap.clear();
    AlbumList createdTagsList;

    // Check if new tags are include in a list of tags hierarchy separated by ','.
    // Ex: /Country/France/people,/City/France/Paris

    TQStringList tagsHierarchies = TQStringList::split(",", tagStr);
    if (tagsHierarchies.isEmpty()) 
        return createdTagsList;

    for (TQStringList::const_iterator it = tagsHierarchies.begin(); it != tagsHierarchies.end(); ++it)
    {
        TQString hierarchy = (*it).stripWhiteSpace();
        if (!hierarchy.isEmpty())
        {
            // Check if new tags is a hierarchy of tags separated by '/'.

            TAlbum *root = 0;

            if (hierarchy.startsWith("/") || !mainRootAlbum)
                root = AlbumManager::instance()->findTAlbum(0);
            else
                root = mainRootAlbum;

            TQStringList tagsList = TQStringList::split("/", hierarchy);
            DDebug() << tagsList << endl;

            if (!tagsList.isEmpty())
            {
                for (TQStringList::iterator it2 = tagsList.begin(); it2 != tagsList.end(); ++it2)
                {
                    TQString tagPath, errMsg;
                    TQString tag = (*it2).stripWhiteSpace();
                    if (root->isRoot())
                        tagPath = TQString("/%1").arg(tag);
                    else
                        tagPath = TQString("%1/%2").arg(root->tagPath()).arg(tag);

                    DDebug() << tag << " :: " << tagPath << endl;

                    if (!tag.isEmpty())
                    {
                        // Tag already exist ?
                        TAlbum* album = AlbumManager::instance()->findTAlbum(tagPath);
                        if (!album)
                        {
                            root = AlbumManager::instance()->createTAlbum(root, tag, icon, errMsg);
                        }
                        else
                        {
                            root = album;
                            if (*it2 == tagsList.last())
                                errMap.insert(tagPath, i18n("Tag name already exists"));
                        }

                        if (root)
                            createdTagsList.append(root);
                    }

                    // Sanity check if tag creation failed.
                    if (!root)
                    {
                        errMap.insert(tagPath, errMsg);
                        break;
                    }
                }
            }
        }
    }

    return createdTagsList;
}

void TagEditDlg::showtagsListCreationError(TQWidget* parent, const TQMap<TQString, TQString>& errMap)
{
    if (!errMap.isEmpty())
    {
        TagsListCreationErrorDialog dlg(parent, errMap);
        dlg.exec();
    }
}

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

TagsListCreationErrorDialog::TagsListCreationErrorDialog(TQWidget* parent, const TQMap<TQString, TQString>& errMap)
                           : KDialogBase(parent, 0, true, 0, Help|Ok, Ok, false)
{
    setHelp("tagscreation.anchor", "digikam");
    setCaption(i18n("Tag creation Error"));

    TQWidget* box      = makeMainWidget();
    TQVBoxLayout* vLay = new TQVBoxLayout(box);

    TQLabel *label       = new TQLabel(i18n("Error been occured during Tag creation:"), box);
    TDEListView *listView = new TDEListView(box);
    listView->addColumn(i18n("Tag Path"));
    listView->addColumn(i18n("Error"));
    listView->setResizeMode(TQListView::LastColumn);

    vLay->addWidget(label);
    vLay->addWidget(listView);
    vLay->setMargin(0);
    vLay->setSpacing(0);

    for (TQMap<TQString, TQString>::const_iterator it = errMap.begin() ; it != errMap.end() ; ++it)
        new TDEListViewItem(listView, it.key(), it.data());

    adjustSize();
}

}  // namespace Digikam