~ubuntu-branches/ubuntu/natty/digikam/natty

« back to all changes in this revision

Viewing changes to utilities/advancedrename/advancedrenameinput.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Luka Renko
  • Date: 2009-11-12 18:02:13 UTC
  • mfrom: (1.2.24 upstream) (3.2.7 sid)
  • Revision ID: james.westby@ubuntu.com-20091112180213-8p63z8taug49ji3t
Tags: 2:1.0.0~beta6-1ubuntu1
* Merge with Debian, remaining changes:
  - Export .pot name and copy to plugins in debian/rules
  - Remove liblqr-1-0-dev from build-deps, not in main

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
// Qt includes
28
28
 
 
29
#include <QFocusEvent>
29
30
#include <QMouseEvent>
30
 
#include <QFocusEvent>
 
31
#include <QPalette>
 
32
#include <QTimer>
31
33
#include <QToolButton>
32
 
#include <QTimer>
33
34
 
34
35
// KDE includes
35
36
 
 
37
#include <kapplication.h>
36
38
#include <klocale.h>
37
 
 
38
 
// Local includes
39
 
 
40
 
#include "parser.h"
 
39
#include <kconfig.h>
 
40
#include <kconfiggroup.h>
41
41
 
42
42
namespace Digikam
43
43
{
44
44
 
45
 
class AdvancedRenameInputPriv
 
45
class AdvancedRenameLineEditPriv
46
46
{
47
47
public:
48
48
 
49
 
    AdvancedRenameInputPriv() :
 
49
    AdvancedRenameLineEditPriv() :
50
50
        userIsTyping(false),
51
51
        userIsHighlighting(false),
52
52
        tokenMarked(false),
54
54
        selectionLength(-1),
55
55
        curCursorPos(-1),
56
56
        parseTimer(0),
57
 
        parser(0)
58
 
    {}
59
 
 
60
 
    bool    userIsTyping;
61
 
    bool    userIsHighlighting;
62
 
    bool    tokenMarked;
63
 
 
64
 
    int     selectionStart;
65
 
    int     selectionLength;
66
 
    int     curCursorPos;
67
 
 
68
 
    QTimer* parseTimer;
69
 
    Parser* parser;
 
57
        parser(0),
 
58
        selectionType(Parser::Text)
 
59
        {}
 
60
 
 
61
    bool                  userIsTyping;
 
62
    bool                  userIsHighlighting;
 
63
    bool                  tokenMarked;
 
64
 
 
65
    int                   selectionStart;
 
66
    int                   selectionLength;
 
67
    int                   curCursorPos;
 
68
 
 
69
    QTimer*               parseTimer;
 
70
    Parser*               parser;
 
71
 
 
72
    Parser::Type selectionType;
70
73
};
71
74
 
72
 
AdvancedRenameInput::AdvancedRenameInput(QWidget* parent)
73
 
                    : KLineEdit(parent), d(new AdvancedRenameInputPriv)
 
75
AdvancedRenameLineEdit::AdvancedRenameLineEdit(QWidget* parent)
 
76
                      : KLineEdit(parent), d(new AdvancedRenameLineEditPriv)
74
77
{
 
78
    d->curCursorPos = cursorPosition();
 
79
 
75
80
    setFocusPolicy(Qt::StrongFocus);
76
81
    setClearButtonShown(true);
77
 
    setCompletionMode(KGlobalSettings::CompletionAuto);
78
 
    setClickMessage(i18n("Enter renaming string (without extension)"));
 
82
    setClickMessage(i18n("Enter renaming string"));
79
83
    setToolTip(i18n("<p>Hold CTRL and move the mouse over the line edit widget to highlight token words.<br/>"
80
 
                    "Hold SHIFT and move the mouse to highlight tokens and its modifiers.<br/>"
 
84
                    "Hold SHIFT and move the mouse to highlight a token and its modifiers.<br/>"
81
85
                    "To mark a token, press the left mouse button while it is highlighted."
82
86
                    "</p>"));
83
87
 
84
 
    d->curCursorPos = cursorPosition();
85
 
 
86
88
    // --------------------------------------------------------
87
89
 
88
90
    d->parseTimer = new QTimer(this);
101
103
            this, SLOT(slotCursorPositionChanged(int, int)));
102
104
}
103
105
 
104
 
AdvancedRenameInput::~AdvancedRenameInput()
 
106
AdvancedRenameLineEdit::~AdvancedRenameLineEdit()
105
107
{
106
108
    delete d;
107
109
}
108
110
 
109
 
void AdvancedRenameInput::setParser(Parser* parser)
 
111
void AdvancedRenameLineEdit::setParser(Parser* parser)
110
112
{
111
113
    if (parser)
112
114
    {
114
116
    }
115
117
}
116
118
 
117
 
void AdvancedRenameInput::mouseMoveEvent(QMouseEvent* e)
 
119
void AdvancedRenameLineEdit::mouseMoveEvent(QMouseEvent* e)
118
120
{
119
121
    KLineEdit::mouseMoveEvent(e);
120
122
    int pos = cursorPositionAt(e->pos());
121
123
 
122
124
    if (e->modifiers() == Qt::ControlModifier)
123
125
    {
124
 
        searchAndHighlightTokens(Token, pos);
 
126
        searchAndHighlightTokens(Parser::Token, pos);
125
127
    }
126
 
//    else if ((e->modifiers() & (Qt::ControlModifier)) &&
127
 
//             (e->modifiers())& (Qt::ShiftModifier))
128
 
//    {
129
 
//        searchAndHighlightTokens(Modifier, pos);
130
 
//    }
131
128
    else if (e->modifiers() & Qt::ShiftModifier)
132
129
    {
133
 
        searchAndHighlightTokens(TokenAndModifiers, pos);
 
130
        searchAndHighlightTokens(Parser::TokenAndModifiers, pos);
134
131
    }
135
132
    else if (d->tokenMarked)
136
133
    {
145
142
    }
146
143
}
147
144
 
148
 
void AdvancedRenameInput::mousePressEvent(QMouseEvent* e)
 
145
void AdvancedRenameLineEdit::mousePressEvent(QMouseEvent* e)
149
146
{
150
 
    if (
151
 
            (e->modifiers() == Qt::ControlModifier)                                             ||
152
 
//            ((e->modifiers() & (Qt::ControlModifier)) && (e->modifiers())& (Qt::ShiftModifier)) ||
153
 
            (e->modifiers() & Qt::ShiftModifier)
154
 
       )
 
147
    if ((e->modifiers() == Qt::ControlModifier) || (e->modifiers() & Qt::ShiftModifier))
155
148
    {
156
149
        if (e->button() == Qt::LeftButton)
157
150
        {
162
155
 
163
156
            if (selectionStart() == d->selectionStart)
164
157
            {
165
 
                d->tokenMarked    = true;
 
158
                d->tokenMarked = true;
166
159
                emit signalTokenMarked(d->tokenMarked);
167
160
            }
168
161
            else
180
173
    }
181
174
}
182
175
 
183
 
void AdvancedRenameInput::leaveEvent(QEvent* e)
 
176
void AdvancedRenameLineEdit::leaveEvent(QEvent* e)
184
177
{
185
178
    rememberSelection();
186
179
    KLineEdit::leaveEvent(e);
187
180
}
188
181
 
189
 
void AdvancedRenameInput::focusInEvent(QFocusEvent* e)
 
182
void AdvancedRenameLineEdit::focusInEvent(QFocusEvent* e)
190
183
{
191
184
    KLineEdit::focusInEvent(e);
192
185
 
196
189
    }
197
190
}
198
191
 
199
 
void AdvancedRenameInput::focusOutEvent(QFocusEvent* e)
 
192
void AdvancedRenameLineEdit::focusOutEvent(QFocusEvent* e)
200
193
{
201
194
    rememberSelection();
202
195
    KLineEdit::focusOutEvent(e);
203
196
}
204
197
 
205
 
void AdvancedRenameInput::rememberSelection()
 
198
void AdvancedRenameLineEdit::rememberSelection()
206
199
{
207
 
    if (hasSelectedText())
 
200
    if ((hasSelectedText() && d->selectionType == Parser::Text) ||
 
201
        (hasSelectedText() && (d->selectionType ==Parser::Token || d->selectionType == Parser::TokenAndModifiers)
 
202
                           && tokenIsSelected()))
208
203
    {
209
204
        d->selectionStart  = selectionStart();
210
205
        d->selectionLength = selectedText().count();
218
213
    }
219
214
}
220
215
 
221
 
void AdvancedRenameInput::searchAndHighlightTokens(SelectionType type, int pos)
 
216
void AdvancedRenameLineEdit::searchAndHighlightTokens(Parser::Type type, int pos)
222
217
{
223
218
    if (d->userIsTyping)
224
219
    {
234
229
    int start;
235
230
    int length;
236
231
 
237
 
    bool found = false;
238
 
 
239
 
    switch (type)
240
 
    {
241
 
        case Token:
242
 
            found = d->parser->tokenAtPosition(text(), pos, start, length);
243
 
            break;
244
 
        case TokenAndModifiers:
245
 
            found = d->parser->tokenModifierAtPosition(text(), pos, start, length);
246
 
            break;
247
 
        default: break;
248
 
    }
 
232
    bool found = d->parser->tokenAtPosition(type, text(), pos, start, length);
249
233
 
250
234
    if (found)
251
235
    {
252
 
        setSelectionColor(type);
253
 
 
254
236
        deselect();
255
237
        setSelection(start, length);
256
238
 
257
239
        d->selectionStart  = start;
258
240
        d->selectionLength = length;
 
241
 
 
242
        d->selectionType = type;
 
243
        setSelectionColor(type);
259
244
    }
260
245
    else
261
246
    {
264
249
    }
265
250
}
266
251
 
267
 
bool AdvancedRenameInput::tokenIsSelected()
 
252
bool AdvancedRenameLineEdit::tokenIsSelected()
268
253
{
269
254
    bool selected = (d->selectionStart != -1) && (d->selectionLength != -1) && d->tokenMarked;
270
255
    return selected;
271
256
}
272
257
 
273
 
void AdvancedRenameInput::slotTextChanged()
 
258
void AdvancedRenameLineEdit::slotTextChanged()
274
259
{
275
260
    d->userIsTyping = true;
276
261
    d->parseTimer->start();
277
262
}
278
263
 
279
 
void AdvancedRenameInput::slotParseTimer()
 
264
void AdvancedRenameLineEdit::slotParseTimer()
280
265
{
281
266
    d->userIsTyping = false;
282
267
    emit signalTextChanged(text());
283
268
}
284
269
 
285
 
void AdvancedRenameInput::slotCursorPositionChanged(int oldPos, int newPos)
 
270
void AdvancedRenameLineEdit::slotCursorPositionChanged(int oldPos, int newPos)
286
271
{
287
272
    Q_UNUSED(oldPos)
288
273
 
293
278
    resetSelection();
294
279
}
295
280
 
296
 
void AdvancedRenameInput::resetSelection()
 
281
void AdvancedRenameLineEdit::resetSelection()
297
282
{
298
283
    d->tokenMarked     = false;
299
284
    d->selectionStart  = -1;
300
285
    d->selectionLength = -1;
301
 
    setSelectionColor(StandardText);
 
286
    d->selectionType   = Parser::Text;
 
287
    setSelectionColor(Parser::Text);
302
288
    emit signalTokenMarked(d->tokenMarked);
303
289
}
304
290
 
305
 
void AdvancedRenameInput::slotAddToken(const QString& token)
 
291
void AdvancedRenameLineEdit::slotAddToken(const QString& token)
306
292
{
307
293
    if (!token.isEmpty())
308
294
    {
326
312
    setFocus();
327
313
}
328
314
 
329
 
void AdvancedRenameInput::slotAddModifier(const QString& token)
 
315
void AdvancedRenameLineEdit::slotAddModifier(const QString& token)
330
316
{
331
317
    if (!token.isEmpty())
332
318
    {
342
328
    setFocus();
343
329
}
344
330
 
345
 
void AdvancedRenameInput::setSelectionColor(SelectionType type)
 
331
void AdvancedRenameLineEdit::setSelectionColor(Parser::Type type)
346
332
{
347
 
    QString cssTemplate("QLineEdit { selection-background-color: %1; selection-color: %2;}");
348
 
    QString css;
 
333
    QPalette p = palette();
349
334
 
350
335
    switch (type)
351
336
    {
352
 
        case Token:
353
 
            css = cssTemplate.arg("red").arg("white");
354
 
            break;
355
 
        case Modifier:
356
 
            css = cssTemplate.arg("green").arg("white");
357
 
            break;
358
 
        case TokenAndModifiers:
359
 
            css = cssTemplate.arg("yellow").arg("black");
360
 
            break;
361
 
        case StandardText:
362
 
            css = cssTemplate.arg("palette(highlight)").arg("palette(highlighted-text)");
 
337
        case Parser::Token:
 
338
            p.setColor(QPalette::Active,   QPalette::Highlight,       Qt::red);
 
339
            p.setColor(QPalette::Active,   QPalette::HighlightedText, Qt::white);
 
340
            p.setColor(QPalette::Inactive, QPalette::Highlight,       Qt::red);
 
341
            p.setColor(QPalette::Inactive, QPalette::HighlightedText, Qt::white);
 
342
            break;
 
343
        case Parser::TokenAndModifiers:
 
344
            p.setColor(QPalette::Active,   QPalette::Highlight,       Qt::yellow);
 
345
            p.setColor(QPalette::Active,   QPalette::HighlightedText, Qt::black);
 
346
            p.setColor(QPalette::Inactive, QPalette::Highlight,       Qt::yellow);
 
347
            p.setColor(QPalette::Inactive, QPalette::HighlightedText, Qt::black);
 
348
            break;
 
349
        case Parser::Text:
 
350
        default:
 
351
            p = kapp->palette();
 
352
            break;
363
353
    }
364
 
    setStyleSheet(css);
 
354
    setPalette(p);
 
355
}
 
356
 
 
357
// --------------------------------------------------------
 
358
 
 
359
class AdvancedRenameInputPriv
 
360
{
 
361
public:
 
362
 
 
363
    AdvancedRenameInputPriv() :
 
364
        configGroupName("AdvancedRename Input"),
 
365
        configPatternHistoryListEntry("Pattern History List"),
 
366
 
 
367
        maxVisibleItems(10),
 
368
        maxHistoryItems(20),
 
369
        lineEdit(0)
 
370
        {}
 
371
 
 
372
    const QString           configGroupName;
 
373
    const QString           configPatternHistoryListEntry;
 
374
 
 
375
    const int               maxVisibleItems;
 
376
    const int               maxHistoryItems;
 
377
 
 
378
    QStringList             patternHistory;
 
379
 
 
380
    AdvancedRenameLineEdit* lineEdit;
 
381
};
 
382
 
 
383
// --------------------------------------------------------
 
384
 
 
385
AdvancedRenameInput::AdvancedRenameInput(QWidget* parent)
 
386
                   : KComboBox(parent), d(new AdvancedRenameInputPriv)
 
387
{
 
388
    // important: setEditable() has to be called before adding the actual line edit widget, otherwise
 
389
    //            our lineEdit gets removed again.
 
390
    setEditable(true);
 
391
    setMaxVisibleItems(d->maxVisibleItems);
 
392
    setMaxCount(d->maxHistoryItems);
 
393
 
 
394
    setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
 
395
 
 
396
    d->lineEdit = new AdvancedRenameLineEdit(this);
 
397
    setLineEdit(d->lineEdit);
 
398
 
 
399
    connect(d->lineEdit, SIGNAL(signalTextChanged(const QString&)),
 
400
            this, SIGNAL(signalTextChanged(const QString&)));
 
401
 
 
402
    connect(d->lineEdit, SIGNAL(signalTokenMarked(bool)),
 
403
            this, SIGNAL(signalTokenMarked(bool)));
 
404
 
 
405
    readSettings();
 
406
}
 
407
 
 
408
AdvancedRenameInput::~AdvancedRenameInput()
 
409
{
 
410
    writeSettings();
 
411
    delete d;
 
412
}
 
413
 
 
414
void AdvancedRenameInput::setParser(Parser* parser)
 
415
{
 
416
    d->lineEdit->setParser(parser);
 
417
}
 
418
 
 
419
void AdvancedRenameInput::slotAddToken(const QString& str)
 
420
{
 
421
    d->lineEdit->slotAddToken(str);
 
422
}
 
423
 
 
424
void AdvancedRenameInput::slotAddModifier(const QString& str)
 
425
{
 
426
    d->lineEdit->slotAddModifier(str);
 
427
}
 
428
 
 
429
void AdvancedRenameInput::readSettings()
 
430
{
 
431
    KSharedConfig::Ptr config = KGlobal::config();
 
432
    KConfigGroup group        = config->group(d->configGroupName);
 
433
 
 
434
    d->patternHistory = group.readEntry(d->configPatternHistoryListEntry, QStringList());
 
435
    d->patternHistory.removeAll(QString(""));
 
436
    addItems(d->patternHistory);
 
437
    d->lineEdit->clear();
 
438
}
 
439
 
 
440
void AdvancedRenameInput::writeSettings()
 
441
{
 
442
    KSharedConfig::Ptr config = KGlobal::config();
 
443
    KConfigGroup group        = config->group(d->configGroupName);
 
444
 
 
445
    // remove duplicate entries and save pattern history, omit empty strings
 
446
    QString pattern = d->lineEdit->text();
 
447
    d->patternHistory.removeAll(pattern);
 
448
    d->patternHistory.removeAll(QString(""));
 
449
    d->patternHistory.prepend(pattern);
 
450
    group.writeEntry(d->configPatternHistoryListEntry, d->patternHistory);
365
451
}
366
452
 
367
453
}  // namespace Digikam