summaryrefslogtreecommitdiffstats
path: root/digikam/imageplugins/inserttext/inserttexttool.cpp
blob: 80b3269046291cb6bb6d93dc95e4df2b488462da (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-02-14
 * Description : a plugin to insert a text over an image.
 *
 * Copyright (C) 2005-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2006-2008 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * 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 <tqbrush.h>
#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqframe.h>
#include <tqhbuttongroup.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpainter.h>
#include <tqpen.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>
#include <tqtimer.h>
#include <tqtooltip.h>
#include <tqvgroupbox.h>
#include <tqwhatsthis.h>

// KDE includes.

#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <kcolorbutton.h>
#include <tdeconfig.h>
#include <kcursor.h>
#include <khelpmenu.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kstandarddirs.h>
#include <ktextedit.h>

// Local includes.

#include "daboutdata.h"
#include "ddebug.h"
#include "dimg.h"
#include "editortoolsettings.h"
#include "fontchooserwidget.h"
#include "imageiface.h"
#include "inserttextwidget.h"
#include "inserttexttool.h"
#include "inserttexttool.moc"

using namespace Digikam;

namespace DigikamInsertTextImagesPlugin
{

InsertTextTool::InsertTextTool(TQObject* parent)
              : EditorTool(parent)
{
    setName("inserttext");
    setToolName(i18n("Insert Text"));
    setToolIcon(SmallIcon("inserttext"));

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

    TQFrame *frame   = new TQFrame(0);
    frame->setFrameStyle(TQFrame::Panel|TQFrame::Sunken);
    TQVBoxLayout* l  = new TQVBoxLayout(frame, 5, 0);
    m_previewWidget = new InsertTextWidget(480, 320, frame);
    l->addWidget(m_previewWidget);
    TQWhatsThis::add(m_previewWidget, i18n("<p>This previews the text inserted in the image. "
                                          "You can use the mouse to move the text to the right location."));
    setToolView(frame);

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

    m_gboxSettings = new EditorToolSettings(EditorToolSettings::Default|
                                            EditorToolSettings::Ok|
                                            EditorToolSettings::Cancel);
    TQGridLayout *grid = new TQGridLayout(m_gboxSettings->plainPage(), 9, 1);

    m_textEdit = new KTextEdit(m_gboxSettings->plainPage());
    m_textEdit->setCheckSpellingEnabled(true);
    m_textEdit->setWordWrap(TQTextEdit::NoWrap);
    TQWhatsThis::add(m_textEdit, i18n("<p>Here, enter the text you want to insert in your image."));

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

    m_fontChooserWidget = new FontChooserWidget(m_gboxSettings->plainPage());
    TQWhatsThis::add( m_textEdit, i18n("<p>Here you can choose the font to be used."));

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

    TDEIconLoader icon;
    m_alignButtonGroup = new TQHButtonGroup(m_gboxSettings->plainPage());

    TQPushButton *alignLeft = new TQPushButton(m_alignButtonGroup);
    m_alignButtonGroup->insert(alignLeft, ALIGN_LEFT);
    alignLeft->setPixmap(icon.loadIcon("text_left", (TDEIcon::Group) TDEIcon::Small));
    alignLeft->setToggleButton(true);
    TQToolTip::add(alignLeft, i18n("Align text to the left"));

    TQPushButton *alignRight = new TQPushButton(m_alignButtonGroup);
    m_alignButtonGroup->insert(alignRight, ALIGN_RIGHT);
    alignRight->setPixmap(icon.loadIcon("text_right", (TDEIcon::Group) TDEIcon::Small));
    alignRight->setToggleButton(true);
    TQToolTip::add(alignRight, i18n("Align text to the right"));

    TQPushButton *alignCenter = new TQPushButton(m_alignButtonGroup);
    m_alignButtonGroup->insert(alignCenter, ALIGN_CENTER);
    alignCenter->setPixmap(icon.loadIcon("text_center", (TDEIcon::Group) TDEIcon::Small));
    alignCenter->setToggleButton(true);
    TQToolTip::add(alignCenter, i18n("Align text to center"));

    TQPushButton *alignBlock = new TQPushButton(m_alignButtonGroup);
    m_alignButtonGroup->insert(alignBlock, ALIGN_BLOCK);
    alignBlock->setPixmap(icon.loadIcon("text_block", (TDEIcon::Group) TDEIcon::Small));
    alignBlock->setToggleButton(true);
    TQToolTip::add(alignBlock, i18n("Align text to a block"));

    m_alignButtonGroup->setExclusive(true);
    m_alignButtonGroup->setFrameShape(TQFrame::NoFrame);

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

    TQLabel *label1 = new TQLabel(i18n("Rotation:"), m_gboxSettings->plainPage());
    m_textRotation = new TQComboBox(false, m_gboxSettings->plainPage());
    m_textRotation->insertItem(i18n("None"));
    m_textRotation->insertItem(i18n("90 Degrees"));
    m_textRotation->insertItem(i18n("180 Degrees"));
    m_textRotation->insertItem(i18n("270 Degrees"));
    TQWhatsThis::add( m_textRotation, i18n("<p>Select the text rotation to use."));

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

    TQLabel *label2    = new TQLabel(i18n("Color:"), m_gboxSettings->plainPage());
    m_fontColorButton = new KColorButton( TQt::black, m_gboxSettings->plainPage() );
    TQWhatsThis::add( m_fontColorButton, i18n("<p>Select the font color to use."));

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

    m_borderText = new TQCheckBox(i18n("Add border"), m_gboxSettings->plainPage());
    TQToolTip::add(m_borderText, i18n("Add a solid border around text using current text color"));

    m_transparentText = new TQCheckBox(i18n("Semi-transparent"), m_gboxSettings->plainPage());
    TQToolTip::add(m_transparentText, i18n("Use semi-transparent text background under image"));

    grid->addMultiCellWidget(m_textEdit,          0, 2, 0, 1);
    grid->addMultiCellWidget(m_fontChooserWidget, 3, 3, 0, 1);
    grid->addMultiCellWidget(m_alignButtonGroup,  4, 4, 0, 1);
    grid->addMultiCellWidget(label1,              5, 5, 0, 0);
    grid->addMultiCellWidget(m_textRotation,      5, 5, 1, 1);
    grid->addMultiCellWidget(label2,              6, 6, 0, 0);
    grid->addMultiCellWidget(m_fontColorButton,   6, 6, 1, 1);
    grid->addMultiCellWidget(m_borderText,        7, 7, 0, 1);
    grid->addMultiCellWidget(m_transparentText,   8, 8, 0, 1);
    grid->setMargin(0);
    grid->setSpacing(m_gboxSettings->spacingHint());
    grid->setRowStretch(9, 10);

    setToolSettings(m_gboxSettings);
    init();

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

    connect(m_fontChooserWidget, TQT_SIGNAL(fontSelected (const TQFont&)),
            this, TQT_SLOT(slotFontPropertiesChanged(const TQFont&)));

    connect(m_fontColorButton, TQT_SIGNAL(changed(const TQColor&)),
            this, TQT_SLOT(slotUpdatePreview()));

    connect(m_textEdit, TQT_SIGNAL(textChanged()),
            this, TQT_SLOT(slotUpdatePreview()));

    connect(m_alignButtonGroup, TQT_SIGNAL(released(int)),
            this, TQT_SLOT(slotAlignModeChanged(int)));

    connect(m_borderText, TQT_SIGNAL(toggled(bool)),
            this, TQT_SLOT(slotUpdatePreview()));

    connect(m_transparentText, TQT_SIGNAL(toggled(bool)),
            this, TQT_SLOT(slotUpdatePreview()));

    connect(m_textRotation, TQT_SIGNAL(activated(int)),
            this, TQT_SLOT(slotUpdatePreview()));

    connect(this, TQT_SIGNAL(signalUpdatePreview()),
            this, TQT_SLOT(slotUpdatePreview()));

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

    slotUpdatePreview();
}

InsertTextTool::~InsertTextTool()
{
}

void InsertTextTool::readSettings()
{
    TDEConfig *config = kapp->config();
    config->setGroup("inserttext Tool");
    TQColor black(0, 0, 0);
    TQFont  defaultFont;

    int orgW = m_previewWidget->imageIface()->originalWidth();
    int orgH = m_previewWidget->imageIface()->originalHeight();

    if (orgW > orgH) m_defaultSizeFont = (int)(orgH / 8.0);
    else m_defaultSizeFont = (int)(orgW / 8.0);

    defaultFont.setPointSize(m_defaultSizeFont);
    m_textRotation->setCurrentItem(config->readNumEntry("Text Rotation", 0));
    m_fontColorButton->setColor(config->readColorEntry("Font Color", &black));
    m_textEdit->setText(config->readEntry("Text String", i18n("Enter your text here!")));
    m_textFont = config->readFontEntry("Font Properties", &defaultFont);
    m_fontChooserWidget->setFont(m_textFont);
    m_alignTextMode = config->readNumEntry("Text Alignment", ALIGN_LEFT);
    m_borderText->setChecked(config->readBoolEntry("Border Text", false));
    m_transparentText->setChecked(config->readBoolEntry("Transparent Text", false));
    m_previewWidget->setPositionHint(config->readRectEntry("Position Hint"));

    static_cast<TQPushButton*>(m_alignButtonGroup->find(m_alignTextMode))->setOn(true);
    slotAlignModeChanged(m_alignTextMode);
}

void InsertTextTool::writeSettings()
{
    TDEConfig *config = kapp->config();
    config->setGroup("inserttext Tool");

    config->writeEntry("Text Rotation", m_textRotation->currentItem());
    config->writeEntry("Font Color", m_fontColorButton->color());
    config->writeEntry("Text String", m_textEdit->text());
    config->writeEntry("Font Properties", m_textFont);
    config->writeEntry("Text Alignment", m_alignTextMode);
    config->writeEntry("Border Text", m_borderText->isChecked());
    config->writeEntry("Transparent Text", m_transparentText->isChecked());
    config->writeEntry("Position Hint", m_previewWidget->getPositionHint());

    config->sync();
}

void InsertTextTool::slotResetSettings()
{
    m_fontColorButton->blockSignals(true);
    m_alignButtonGroup->blockSignals(true);
    m_fontChooserWidget->blockSignals(true);

    m_textRotation->setCurrentItem(0); // No rotation.
    m_fontColorButton->setColor(TQt::black);
    TQFont defaultFont;
    m_textFont = defaultFont; // Reset to default TDE font.
    m_textFont.setPointSize(m_defaultSizeFont);
    m_fontChooserWidget->setFont(m_textFont);
    m_borderText->setChecked(false);
    m_transparentText->setChecked(false);
    m_previewWidget->resetEdit();
    static_cast<TQPushButton*> (m_alignButtonGroup->find(ALIGN_LEFT))->setOn(true);

    m_fontChooserWidget->blockSignals(false);
    m_fontColorButton->blockSignals(false);
    m_alignButtonGroup->blockSignals(false);
    slotAlignModeChanged(ALIGN_LEFT);
}

void InsertTextTool::slotAlignModeChanged(int mode)
{
    m_alignTextMode = mode;
    m_textEdit->selectAll(true);

    switch (m_alignTextMode)
        {
        case ALIGN_LEFT:
           m_textEdit->setAlignment( TQt::AlignLeft );
           break;

        case ALIGN_RIGHT:
           m_textEdit->setAlignment( TQt::AlignRight );
           break;

        case ALIGN_CENTER:
           m_textEdit->setAlignment( TQt::AlignHCenter );
           break;

        case ALIGN_BLOCK:
           m_textEdit->setAlignment( TQt::AlignJustify );
           break;
        }

    m_textEdit->selectAll(false);
    emit signalUpdatePreview();
}

void InsertTextTool::slotFontPropertiesChanged(const TQFont& font)
{
    m_textFont = font;
    emit signalUpdatePreview();
}

void InsertTextTool::slotUpdatePreview()
{
    m_previewWidget->setText(m_textEdit->text(), m_textFont, m_fontColorButton->color(), m_alignTextMode,
                             m_borderText->isChecked(), m_transparentText->isChecked(),
                             m_textRotation->currentItem());
}

void InsertTextTool::finalRendering()
{
    kapp->setOverrideCursor(KCursor::waitCursor());

    ImageIface iface(0, 0);
    DImg dest = m_previewWidget->makeInsertText();
    iface.putOriginalImage(i18n("Insert Text"), dest.bits(), dest.width(), dest.height());

    kapp->restoreOverrideCursor();
}

}  // NameSpace DigikamInsertTextImagesPlugin