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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* ============================================================
 *
 * This file is a part of Tumorprofil
 *
 * Date        : 11.09.2015
 *
 * SPDX-FileCopyrightText: 2012 by Marcel Wiesweg <marcel dot wiesweg at uk-essen dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "taggingactionfactory.h"

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "tagscache.h"
#include "coredbaccess.h"
#include "coredb.h"

namespace Digikam
{

class Q_DECL_HIDDEN TaggingActionFactory::Private
{
public:

    Private() = default;

public:

    QString              fragment;
    int                  parentTagId            = 0;
    NameMatchMode        nameMatchMode          = MatchStartingWithFragment;
    ConstraintInterface* constraintInterface    = nullptr;

    int                  defaultIndex           = -1;   ///< use if valid is true
    int                  recentIndex            = 0;    ///< use if valid is true

    QList<TaggingAction> actions;                       ///< use if valid is true
    bool                 valid                  = false;

    TaggingAction        defaultAction;                 ///< independent from valid

public:

    void invalidate()
    {
        valid         = false;
        actions.clear();
        defaultAction = TaggingAction();
    }
};

TaggingActionFactory::TaggingActionFactory()
    : d(new Private)
{
}

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

QString TaggingActionFactory::fragment() const
{
    return d->fragment;
}

void TaggingActionFactory::setFragment(const QString& fragment)<--- Shadow argument
{
    if (fragment == d->fragment)
    {
        return;
    }

    d->fragment = fragment;
    d->invalidate();
}

int TaggingActionFactory::parentTagId() const
{
    return d->parentTagId;
}

void TaggingActionFactory::setParentTag(int parentTagId)<--- Shadow argument
{
    if (parentTagId == d->parentTagId)
    {
        return;
    }

    d->parentTagId = parentTagId;
    d->invalidate();
}

void TaggingActionFactory::setConstraintInterface(ConstraintInterface* const iface)
{
    if (d->constraintInterface == iface)
    {
        return;
    }

    d->constraintInterface = iface;
    d->invalidate();
}

TaggingActionFactory::ConstraintInterface* TaggingActionFactory::constraintInterface() const
{
    return d->constraintInterface;
}

void TaggingActionFactory::setNameMatchMode(NameMatchMode mode)
{
    if (d->nameMatchMode == mode)
    {
        return;
    }

    d->nameMatchMode = mode;
    d->invalidate();
}

TaggingActionFactory::NameMatchMode TaggingActionFactory::nameMatchMode() const
{
    return d->nameMatchMode;
}

void TaggingActionFactory::reset()
{
    d->fragment.clear();
    d->parentTagId         = 0;
    d->defaultIndex        = -1;
    d->nameMatchMode       = MatchStartingWithFragment;
    d->constraintInterface = nullptr;
    d->invalidate();
}

QList<TaggingAction> TaggingActionFactory::actions() const
{
    if (d->valid)
    {
        return d->actions;
    }

    QList<TaggingAction> actions;
    int defaultActionIndex = 0;
    d->recentIndex         = 0;

    // We use this action to find the right entry to select

    TaggingAction defaultAction = defaultTaggingAction();
    TaggingAction newUnderParent;

    if (d->parentTagId)
    {
        newUnderParent = TaggingAction(d->fragment, d->parentTagId);
    }

    TaggingAction newToplevel(d->fragment, 0);

    QList<int> completionEntries;

    if (d->nameMatchMode == MatchStartingWithFragment)
    {
        completionEntries = TagsCache::instance()->tagsStartingWith(d->fragment);
    }
    else
    {
        completionEntries = TagsCache::instance()->tagsContaining(d->fragment);
    }

    QList<TaggingAction> assignActions;

    // order the matched tags by chronological order, based on
    // recently assigned tags

    QList<int> recentTagIDs = CoreDbAccess().db()->getRecentlyAssignedTags();

    QListIterator<int> recent_iter(recentTagIDs);
    recent_iter.toBack();

    while (recent_iter.hasPrevious())
    {
        int pos = completionEntries.indexOf(recent_iter.previous());

        if (pos > 0)
        {
            completionEntries.move(pos, 0);
            ++d->recentIndex;
        }
    }

    for (int id : std::as_const(completionEntries))
    {
        if (d->constraintInterface && !d->constraintInterface->matches(id))
        {
            continue;
        }

        assignActions << TaggingAction(id);
    }

    if (defaultAction.shallCreateNewTag())
    {
        // If it is the default action, we place the "Create Tag" entry at the top of the list.

        if (newUnderParent.isValid() && newUnderParent == defaultAction)
        {
            // Case A
            // a tag is currently selected in the listbox, we have the choice of toplevel and underparent for a new tag
            // the entire text currently written by the user doesn't exist as a tag. However, it might be a part of a tag

            for (const TaggingAction& assignAction : std::as_const(assignActions))
            {
                actions << assignAction;
            }

            actions << newUnderParent;
            actions << newToplevel;
        }
        else
/*
        if (createItemTopLevel && (createItemTopLevel->action() == defaultAction))
*/
        {
            // Case B
            // no tag is currently selected in the listbox, only toplevel choice for a new tag
            // the entire text currently written by the user doesn't exist as a tag. However, it might be a part of a tag

            for (const TaggingAction& assignAction : std::as_const(assignActions))
            {
                actions << assignAction;
            }

            actions << newToplevel;
        }
    }
    else
    {
        // Case C
        // the entire text currently written by the user exists as a tag

        for (const TaggingAction& assignAction : std::as_const(assignActions))
        {
            actions << assignAction;

            if (assignAction == defaultAction)
            {
                defaultActionIndex = actions.size()-1;
            }
        }

        actions << newUnderParent;
        actions << newToplevel;
    }

    Private* const ncd = const_cast<Private*>(d);
    ncd->valid         = true;
    ncd->actions       = actions;
    ncd->defaultIndex  = defaultActionIndex;

    return actions;
}

int TaggingActionFactory::indexOfDefaultAction() const
{
    if (!d->valid)
    {
        actions();
    }

    return d->defaultIndex;
}

int TaggingActionFactory::indexOfLastRecentAction() const
{
    if (!d->valid)
    {
        return 0;
    }

    return d->recentIndex;
}

/*
AddTagsCompletionBoxItem* AddTagsCompletionBox::Private::createItemForExistingTag(TAlbum* talbum, bool uniqueName)
{
    if (!talbum || talbum->isRoot())
    {
        return 0;
    }

    AddTagsCompletionBoxItem* const item = new AddTagsCompletionBoxItem;
    TAlbum* const parent                 = static_cast<TAlbum*>(talbum->parent());

    if (parent->isRoot() || uniqueName)
    {
        item->setText(talbum->title());
    }
    else
    {
        item->setText(i18nc("<tag name> in <tag path>", "%1 in %2",
                            talbum->title(), parent->tagPath(false)));
    }

    if (model || filterModel)
    {
        QModelIndex index;

        if (filterModel)
        {
            index = filterModel->indexForAlbum(talbum);
        }
        else if (model)
        {
            index = model->indexForAlbum(talbum);
        }

        item->setData(Qt::DecorationRole, index.data(Qt::DecorationRole));
    }

    item->setData(CompletionTextRole, talbum->title());
    item->setAction(TaggingAction(talbum->id()));

    return item;
}

AddTagsCompletionBoxItem* AddTagsCompletionBox::Private::createItemForNewTag(const QString& newName, TAlbum* parent)
{
    int parentTagId = parent ? parent->id() : 0;

    // If tag exists, do not add an entry to create it

    if (TagsCache::instance()->tagForName(newName, parentTagId))
    {
        return 0;
    }

    AddTagsCompletionBoxItem* const item = new AddTagsCompletionBoxItem;

    item->setData(Qt::DecorationRole, AlbumThumbnailLoader::instance()->getNewTagIcon());
    item->setData(CompletionTextRole, newName);
    item->setAction(TaggingAction(newName, parentTagId));

    return item;
}
*/

QString TaggingActionFactory::suggestedUIString(const TaggingAction& action) const
{
    if (!action.isValid())
    {
        return QString();
    }

    if (action.shallAssignTag())
    {
        QString tagName     = TagsCache::instance()->tagName(action.tagId());
        int tagsParentTagId = TagsCache::instance()->parentTag(action.tagId());

        // check if it is a toplevel tag or there is only one tag with this name - then simply the tag name is sufficient

        if ((tagsParentTagId == 0) || (TagsCache::instance()->tagsForName(tagName).size() == 1))
        {
            return tagName;
        }
        else
        {
            return i18nc("<tag name> in <tag path>", "%1 in %2",
                         tagName, TagsCache::instance()->tagPath(tagsParentTagId, TagsCache::NoLeadingSlash));
        }
    }
    else // shallCreateNewTag
    {
        if (action.parentTagId())
        {
            return i18nc("Create New Tag <tag name> in <parent tag path>", "Create \"%1\" in %2",
                         action.newTagName(), TagsCache::instance()->tagPath(action.parentTagId(), TagsCache::NoLeadingSlash));
        }
        else
        {
            return i18n("Create \"%1\"", action.newTagName());
        }
    }
}

TaggingAction TaggingActionFactory::defaultTaggingAction() const
{
    if (d->defaultAction.isValid())
    {
        return d->defaultAction;
    }

    if (d->fragment.isEmpty())
    {
        return TaggingAction();
    }

    Private* const ncd = const_cast<Private*>(d);
    ncd->defaultAction = defaultTaggingAction(d->fragment, d->parentTagId);

    return d->defaultAction;
}

TaggingAction TaggingActionFactory::defaultTaggingAction(const QString& tagName, int parentTagId)
{
    // We now take the presumably best action, without autocompletion popup.
    // 1. Tag exists?
    //    a) Single tag? Assign.
    //    b) Multiple tags? 1. Existing tag under parent. 2. Toplevel tag 3. Alphabetically lowest tag
    // 2. Create tag under parent. No parent selected? Toplevel

    if (tagName.isEmpty())
    {
        return TaggingAction();
    }

    QList<int> tagIds = TagsCache::instance()->tagsForName(tagName);

    if (!tagIds.isEmpty())
    {
        if (tagIds.count() == 1)
        {
            return TaggingAction(tagIds.first());
        }
        else
        {
            int tagId = 0;

            if (parentTagId)
            {
                tagId = TagsCache::instance()->tagForName(tagName, parentTagId);
            }

            if (!tagId)
            {
                tagId = TagsCache::instance()->tagForName(tagName);    // toplevel tag
            }

            if (!tagId)
            {
                // sort lexically

                QMap<QString, int> map;

                for (int id : std::as_const(tagIds))
                {
                    map[TagsCache::instance()->tagPath(id, TagsCache::NoLeadingSlash)] = id;
                }

                tagId = map.begin().value();
            }

            return TaggingAction(tagId);
        }
    }
    else
    {
        return TaggingAction(tagName, parentTagId);
    }
}

} // namespace Digikam