1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2009-08-08
7  * Description : an option to add a sequence number to the parser
8  *
9  * Copyright (C) 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "sequencenumberoption.h"
25 
26 // Qt includes
27 
28 #include <QGridLayout>
29 #include <QGroupBox>
30 #include <QLabel>
31 #include <QPointer>
32 
33 // KDE includes
34 
35 #include <klocalizedstring.h>
36 
37 // Local includes
38 
39 #include "ui_sequencenumberoptiondialogwidget.h"
40 
41 namespace Digikam
42 {
43 
SequenceNumberDialog(Rule * const parent)44 SequenceNumberDialog::SequenceNumberDialog(Rule* const parent)
45     : RuleDialog(parent),
46       ui(new Ui::SequenceNumberOptionDialogWidget())
47 {
48     QWidget* const mainWidget = new QWidget(this);
49     ui->setupUi(mainWidget);
50     setSettingsWidget(mainWidget);
51     ui->digits->setFocus();
52 }
53 
~SequenceNumberDialog()54 SequenceNumberDialog::~SequenceNumberDialog()
55 {
56     delete ui;
57 }
58 
59 // --------------------------------------------------------
60 
SequenceNumberOption()61 SequenceNumberOption::SequenceNumberOption()
62     : Option(i18nc("Sequence Number", "Number..."),
63              i18n("Add a sequence number"),
64              QLatin1String("accessories-calculator"))
65 {
66     addToken(QLatin1String("#"),                                 i18n("Sequence number"));
67     addToken(QLatin1String("#[||options||]"),                    i18n("Sequence number (||options||: ||e|| = extension aware, ||f|| = folder aware)"));
68     addToken(QLatin1String("#[||options||,||start||]"),          i18n("Sequence number (custom start)"));
69     addToken(QLatin1String("#[||options||,||start||,||step||]"), i18n("Sequence number (custom start + step)"));
70 
71     QRegExp reg(QLatin1String("(#+)(\\[(e?f?,?)?((-?\\d+)(,(-?\\d+))?)?\\])?"));
72     setRegExp(reg);
73 }
74 
~SequenceNumberOption()75 SequenceNumberOption::~SequenceNumberOption()
76 {
77 }
78 
slotTokenTriggered(const QString & token)79 void SequenceNumberOption::slotTokenTriggered(const QString& token)
80 {
81     Q_UNUSED(token)
82 
83     QPointer<SequenceNumberDialog> dlg = new SequenceNumberDialog(this);
84 
85     QString result;
86 
87     if (dlg->exec() == QDialog::Accepted)
88     {
89         int digits          = dlg->ui->digits->value();
90         int start           = dlg->ui->start->value();
91         int step            = dlg->ui->step->value();
92         bool extensionAware = dlg->ui->extensionAware->isChecked();
93         bool folderAware    = dlg->ui->folderAware->isChecked();
94 
95         result = QString::fromUtf8("%1").arg(QLatin1String("#"), digits, QLatin1Char('#'));
96 
97         if ((start > 1) || (step > 1) || extensionAware || folderAware)
98         {
99             result.append(QLatin1Char('['));
100 
101             if (extensionAware)
102             {
103                 result.append(QLatin1Char('e'));
104             }
105 
106             if (folderAware)
107             {
108                 result.append(QLatin1Char('f'));
109             }
110 
111             if ((start > 1) || (step > 1))
112             {
113                 if (extensionAware)
114                 {
115                     result.append(QLatin1Char(','));
116                 }
117 
118                 result.append(QString::number(start));
119             }
120 
121             if (step > 1)
122             {
123                 result.append(QString::fromUtf8(",%1").arg(QString::number(step)));
124             }
125 
126             result.append(QLatin1Char(']'));
127         }
128     }
129 
130     delete dlg;
131 
132     emit signalTokenTriggered(result);
133 }
134 
parseOperation(ParseSettings & settings)135 QString SequenceNumberOption::parseOperation(ParseSettings& settings)
136 {
137     QString result;
138     const QRegExp& reg = regExp();
139     int slength        = 0;
140     int start          = 0;
141     int step           = 0;
142     int number         = 0;
143     int index          = 0;
144 
145     if (settings.manager)
146     {
147         bool extAware    = !reg.cap(3).isEmpty() && reg.cap(3).contains(QLatin1Char('e'));
148         bool folderAware = !reg.cap(3).isEmpty() && reg.cap(3).contains(QLatin1Char('f'));
149 
150         index = settings.manager->indexOfFile(settings.fileUrl.toLocalFile());
151 
152         if (extAware)
153         {
154             index = settings.manager->indexOfFileGroup(settings.fileUrl.toLocalFile());
155         }
156 
157         if (folderAware)
158         {
159             index = settings.manager->indexOfFolder(settings.fileUrl.toLocalFile());
160         }
161     }
162 
163     // --------------------------------------------------------
164 
165     slength = reg.cap(1).length();
166     start   = reg.cap(5).isEmpty() ? settings.startIndex : reg.cap(5).toInt();
167     step    = reg.cap(7).isEmpty() ? 1 : reg.cap(7).toInt();
168 
169     if (start < 1)
170     {
171         start = settings.startIndex;
172     }
173 
174     if (step < 1)
175     {
176         step = 1;
177     }
178 
179     number  = start + ((index - 1) * step);
180     result  = QString::fromUtf8("%1").arg(number, slength, 10, QLatin1Char('0'));
181 
182     return result;
183 }
184 
185 } // namespace Digikam
186