summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/tools/defaulttools/kis_tool_text.cpp
blob: 2e77c0c01f88689e6b520b4e3d74ce50eac93b67 (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
/*
 *  Copyright (c) 2004 Bart Coppens <kde@bartcoppens.be>
 *
 *  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 of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <tqfont.h>
#include <tqrect.h>
#include <tqimage.h>
#include <tqlayout.h>
#include <tqwidget.h>
#include <tqstring.h>
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqpushbutton.h>
#include <tqfontmetrics.h>
#include <tqhbox.h>

#include <tdeaction.h>
#include <kinputdialog.h>
#include <tdelocale.h>
#include <tdefontdialog.h>
#include <ksqueezedtextlabel.h>

#include <tqcolor.h>

#include "kis_point.h"
#include "kis_image.h"
#include "kis_layer.h"
#include "kis_group_layer.h"
#include "kis_paint_layer.h"
#include "kis_cursor.h"
#include "kis_tool_text.h"
#include "kis_paint_device.h"
#include "kis_canvas_subject.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_color.h"
#include "kis_undo_adapter.h"

KisToolText::KisToolText()
    : super(i18n("Text"))
    , m_wasPressed( false )
    , m_windowIsBeingShown( false )
{
    setName("tool_text");
    m_subject = 0;
    setCursor(KisCursor::load("tool_text_cursor.png", 6, 6));
}

KisToolText::~KisToolText()
{
}

void KisToolText::update(KisCanvasSubject *subject)
{
    m_subject = subject;
    super::update(subject);
}

void KisToolText::buttonPress(KisButtonPressEvent *e)
{
    if (m_subject && e->button() == TQt::LeftButton) {
        m_wasPressed = true;
    }
}

void KisToolText::buttonRelease(KisButtonReleaseEvent *e)
{
    if ( m_windowIsBeingShown ) return;

    if (m_subject && e->button() == TQt::LeftButton) {
        if(!m_wasPressed) return;
        m_wasPressed = false;
        KisImageSP img = m_subject->currentImg();

        m_windowIsBeingShown = true;
        bool ok;
        TQString text = KInputDialog::getText(i18n("Font Tool"), i18n("Enter text:"),
             TQString(), &ok);
        if (!ok) {
            m_windowIsBeingShown = false;
            return;
        }

        KisUndoAdapter *undoAdapter = img->undoAdapter();
        if (undoAdapter) {
            undoAdapter->beginMacro(i18n("Text"));
        }

        TQFontMetrics metrics(m_font);
        TQRect boundingRect = metrics.boundingRect(text).normalize();
        int xB = - boundingRect.x();
        int yB = - boundingRect.y();

        if (boundingRect.x() < 0 || boundingRect.y() < 0)
            boundingRect.moveBy(- boundingRect.x(), - boundingRect.y());

        TQPixmap pixels(boundingRect.width(), boundingRect.height());
        {
            TQPainter paint(&pixels);
            paint.fillRect(boundingRect, TQt::white);
            paint.setFont(m_font);
            paint.setBrush(TQBrush(TQt::black));
            paint.drawText(xB, yB, text);
        }
        TQImage image = pixels.convertToImage();

        TQ_INT32 height = boundingRect.height();
        TQ_INT32 width = boundingRect.width();
        KisPaintLayer *layer = new KisPaintLayer(img, '"' + text + '"', OPACITY_OPAQUE);
        KisGroupLayerSP parent = img->rootLayer();
        if (img->activeLayer())
            parent = img->activeLayer()->parent();
        img->addLayer(layer, parent, img->activeLayer());
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                TQRgb pixel = image.pixel(x, y);
                 // use the 'blackness' as alpha :)
                TQ_UINT8 alpha = 255 - tqRed(pixel) * OPACITY_OPAQUE / 255;
                TQColor c = m_subject->fgColor().toTQColor();
                layer->paintDevice()->setPixel(x, y, c, alpha);
            }
        }

        layer->setOpacity(m_opacity);
        layer->setCompositeOp(m_compositeOp);

        layer->setVisible(false);
        TQ_INT32 x = TQMAX(0, static_cast<int>(e->x() - width/2));
        TQ_INT32 y = TQMAX(0, static_cast<int>(e->y() - height/2));
        layer->setX(x);
        layer->setY(y);
        layer->setVisible(true);
        layer->setDirty();

        if (undoAdapter) {
            undoAdapter->endMacro();
        }

        m_windowIsBeingShown = false;
    }
}

void KisToolText::setFont() {
    TDEFontDialog::getFont( m_font, false/*, TQWidget* parent! */ );
    m_lbFontName->setText(TQString(m_font.family() + ", %1").arg(m_font.pointSize()));
}

TQWidget* KisToolText::createOptionWidget(TQWidget* parent)
{
    TQWidget *widget = super::createOptionWidget(parent);

    m_lbFont = new TQLabel(i18n("Font: "), widget);

    TQHBox *fontBox = new TQHBox(widget);
    m_lbFontName = new KSqueezedTextLabel(TQString(m_font.family() + ", %1")
        .arg(m_font.pointSize()), fontBox);
    m_btnMoreFonts = new TQPushButton("...", fontBox);

    connect(m_btnMoreFonts, TQ_SIGNAL(released()), this, TQ_SLOT(setFont()));

    addOptionWidgetOption(fontBox, m_lbFont);

    return widget;
}

void KisToolText::setup(TDEActionCollection *collection)
{
    m_action = static_cast<TDERadioAction *>(collection->action(name()));

    if (m_action == 0) {
        m_action = new TDERadioAction(i18n("T&ext"),
                        "tool_text",
                        TQt::SHIFT+TQt::Key_T,
                        this,
                        TQ_SLOT(activate()),
                        collection,
                        name());
        m_action->setExclusiveGroup("tools");
        m_action->setToolTip(i18n("Text"));
        m_ownAction = true;
    }
}

#include "kis_tool_text.moc"