summaryrefslogtreecommitdiffstats
path: root/lib/koproperty/editors/booledit.cpp
blob: 7791730c7373ec984faf8c2b260a7c97ea63f684 (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
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
   Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "booledit.h"
#include "../property.h"

#include <kiconloader.h>
#include <klocale.h>
#include <kcombobox.h>
#include <kdebug.h>

#include <tqtoolbutton.h>
#include <tqpainter.h>
#include <tqvariant.h>
#include <tqlayout.h>
#include <tqbitmap.h>

using namespace KoProperty;

BoolEdit::BoolEdit(Property *property, TQWidget *parent, const char *name)
 : Widget(property, parent, name)
 , m_yesIcon( SmallIcon("button_ok") )
 , m_noIcon( SmallIcon("button_no") )
{
    m_toggle = new TQToolButton(this);
    m_toggle->setToggleButton( true );
    m_toggle->setFocusPolicy(TQ_WheelFocus);
    m_toggle->setUsesTextLabel(true);
    m_toggle->setTextPosition(TQToolButton::Right);
    m_toggle->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
    //we're not using layout to because of problems with button size
    m_toggle->move(0, 0);
    m_toggle->resize(width(), height());
    setFocusWidget(m_toggle);
    connect(m_toggle, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(slotValueChanged(int)));
}

BoolEdit::~BoolEdit()
{
}

TQVariant
BoolEdit::value() const
{
    return TQVariant(m_toggle->isOn(), 4);
}

void
BoolEdit::setValue(const TQVariant &value, bool emitChange)
{
    m_toggle->blockSignals(true);
    m_toggle->setOn(value.toBool());
    setState( value.toBool() ? TQButton::On : TQButton::Off );
    m_toggle->blockSignals(false);
    if (emitChange)
        emit valueChanged(this);
}

void
BoolEdit::slotValueChanged(int state)
{
    setState(state);
    emit valueChanged(this);
}

static void drawViewerInternal(TQPainter *p, const TQRect &r, const TQVariant &value,
 const TQPixmap& yesIcon, const TQPixmap& noIcon, const TQString& nullText)
{
    p->eraseRect(r);
    TQRect r2(r);
    r2.moveLeft(KIcon::SizeSmall + 6);

    if(value.isNull() && !nullText.isEmpty()) {
        p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, nullText);
    }
    else if(value.toBool()) {
        p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, yesIcon);
        p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, i18n("Yes"));
    }
    else {
        p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, noIcon);
        p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, i18n("No"));
    }
}

void
BoolEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
{
	Q_UNUSED(cg);
	drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, "");
}

void
BoolEdit::setState(int state)
{
    if(TQButton::On == state) {
        m_toggle->setIconSet(TQIconSet(m_yesIcon));
        m_toggle->setTextLabel(i18n("Yes"));
    }
    else if (TQButton::Off == state) {
        m_toggle->setIconSet(TQIconSet(m_noIcon));
        m_toggle->setTextLabel(i18n("No"));
    }
}

void
BoolEdit::resizeEvent(TQResizeEvent *ev)
{
    m_toggle->resize(ev->size());
}

bool
BoolEdit::eventFilter(TQObject* watched, TQEvent* e)
{
    if(e->type() == TQEvent::KeyPress) {
        TQKeyEvent* ev = TQT_TQKEYEVENT(e);
        const int k = ev->key();
        if(k == TQt::Key_Space || k == TQt::Key_Enter || k == TQt::Key_Return) {
            if (m_toggle)
                m_toggle->toggle();
            return true;
        }
    }
    return Widget::eventFilter(watched, e);
}

void
BoolEdit::setReadOnlyInternal(bool readOnly)
{
    setVisibleFlag(!readOnly);
}

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

ThreeStateBoolEdit::ThreeStateBoolEdit(Property *property, TQWidget *parent, const char *name)
 : ComboBox(property, parent, name)
 , m_yesIcon( SmallIcon("button_ok") )
 , m_noIcon( SmallIcon("button_no") )
{
	m_edit->insertItem( m_yesIcon, i18n("Yes") );
	m_edit->insertItem( m_noIcon, i18n("No") );
	TQVariant thirdState = property ? property->option("3rdState") : TQVariant();
	TQPixmap nullIcon( m_yesIcon.size() ); //transparent pixmap of appropriate size
	nullIcon.setMask(TQBitmap(m_yesIcon.size(), true));
	m_edit->insertItem( nullIcon, thirdState.toString().isEmpty() ? i18n("None") : thirdState.toString() );
}

ThreeStateBoolEdit::~ThreeStateBoolEdit()
{
}

TQVariant
ThreeStateBoolEdit::value() const
{
	// list items: true, false, NULL
	const int idx = m_edit->currentItem();
	if (idx==0)
		return TQVariant(true, 1);
	else
		return idx==1 ? TQVariant(false) : TQVariant();
}

void
ThreeStateBoolEdit::setProperty(Property *prop)
{
	m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
	Widget::setProperty(prop);
	m_setValueEnabled = true;
	if(prop)
		setValue(prop->value(), false); //now the value can be set
}

void
ThreeStateBoolEdit::setValue(const TQVariant &value, bool emitChange)
{
	if (!m_setValueEnabled)
		return;

	if (value.isNull())
		m_edit->setCurrentItem(2);
	else
		m_edit->setCurrentItem(value.toBool() ? 0 : 1);

	if (emitChange)
		emit valueChanged(this);
}

void
ThreeStateBoolEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
{
	Q_UNUSED(cg);
	drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, m_edit->text(2));
}

#include "booledit.moc"