summaryrefslogtreecommitdiffstats
path: root/src/abakuslistview.cpp
blob: 477623bce9ee92b8be1b3256cbdee0f8d90646d0 (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
/*
 * abakuslistview.cpp - part of abakus
 * Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
 *
 * 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 <klocale.h>
#include <kpopupmenu.h>
#include <kdebug.h>

#include <tqdragobject.h>
#include <tqcursor.h>
#include <tqheader.h>

#include "dragsupport.h"
#include "abakuslistview.h"
#include "valuemanager.h"
#include "function.h"

ListView::ListView(TQWidget *parent, const char *name) :
    KListView(parent, name), m_menu(0), m_usePopup(false), m_removeSingleId(0),
    m_removeAllId(0)
{
    setResizeMode(LastColumn);
    setDragEnabled(true);

    connect(this, TQT_SIGNAL(contextMenuRequested(TQListViewItem *, const TQPoint &, int)),
	          TQT_SLOT(rightClicked(TQListViewItem *, const TQPoint &)));
}

TQDragObject *ListView::dragObject()
{
    TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
    TQListViewItem *item = itemAt(viewportPos);

    if(!item)
	return 0;

    int column = header()->sectionAt(viewportPos.x());
    TQString dragText = item->text(column);

    TQDragObject *drag = new TQTextDrag(dragText, this, "list item drag");
    drag->setPixmap(DragSupport::makePixmap(dragText, font()));

    return drag;
}

void ListView::enablePopupHandler(bool enable)
{
    if(enable == m_usePopup)
	return;

    m_usePopup = enable;

    if(m_usePopup) {
	if(m_menu)
	    kdError() << "ListView menu shouldn't exist here!\n";

	m_menu = new KPopupMenu(this);

	m_removeSingleId = m_menu->insertItem(removeItemString(), this, TQT_SLOT(removeSelected()));
	m_removeAllId = m_menu->insertItem("Placeholder", this, TQT_SLOT(removeAllItems()));
    }
    else {
	delete m_menu;
	m_menu = 0;
    }
}

TQString ListView::removeItemString() const
{
    return TQString();
}

TQString ListView::removeAllItemsString(unsigned count) const
{
    Q_UNUSED(count);

    return TQString();
}

void ListView::removeSelectedItem(TQListViewItem *item)
{
    Q_UNUSED(item);
}

void ListView::removeAllItems()
{
}

bool ListView::isItemRemovable(TQListViewItem *item) const
{
    Q_UNUSED(item);

    return false;
}

void ListView::rightClicked(TQListViewItem *item, const TQPoint &pt)
{
    if(!m_usePopup)
	return;

    m_menu->setItemEnabled(m_removeSingleId, item && isItemRemovable(item));
    m_menu->changeItem(m_removeAllId, removeAllItemsString(childCount()));
    m_menu->popup(pt);
}

void ListView::removeSelected()
{
    removeSelectedItem(selectedItem());
}

ValueListViewItem::ValueListViewItem(TQListView *listView, const TQString &name,
	const Abakus::number_t &value) :
    KListViewItem(listView, name), m_value(value)
{
    valueChanged();
}

void ValueListViewItem::valueChanged()
{
    setText(1, m_value.toString());
    repaint();
}

void ValueListViewItem::valueChanged(const Abakus::number_t &newValue)
{
    m_value = newValue;

    valueChanged();
}

Abakus::number_t ValueListViewItem::itemValue() const
{
    return m_value;
}

VariableListView::VariableListView(TQWidget *parent, const char *name) :
    ListView(parent, name)
{
    enablePopupHandler(true);
}

TQString VariableListView::removeItemString() const
{
    return i18n("Remove selected variable");
}

TQString VariableListView::removeAllItemsString(unsigned count) const
{
    // count is unreliable because not all of the elements in the list view
    // can be removed.
    count = 0;
    TQStringList values = ValueManager::instance()->valueNames();

    for(TQStringList::ConstIterator value = values.constBegin(); value != values.constEnd(); ++value)
	if(!ValueManager::instance()->isValueReadOnly(*value))
	    ++count;

    return i18n("Remove all variables (1 variable)",
	        "Remove all variables (%n variables)",
		count);
}

bool VariableListView::isItemRemovable(TQListViewItem *item) const
{
    return !ValueManager::instance()->isValueReadOnly(item->text(0));
}

void VariableListView::removeSelectedItem(TQListViewItem *item)
{
    ValueManager::instance()->removeValue(item->text(0));
}

void VariableListView::removeAllItems()
{
    ValueManager::instance()->slotRemoveUserVariables();
}

FunctionListView::FunctionListView(TQWidget *parent, const char *name) :
    ListView(parent, name)
{
    enablePopupHandler(true);
}

TQString FunctionListView::removeItemString() const
{
    return i18n("Remove selected function");
}

TQString FunctionListView::removeAllItemsString(unsigned count) const
{
    return i18n("Remove all functions (1 function)",
	        "Remove all functions (%n functions)",
		count);
}

bool FunctionListView::isItemRemovable(TQListViewItem *item) const
{
    return true;
}

void FunctionListView::removeSelectedItem(TQListViewItem *item)
{
    // Use section to get the beginning of the string up to (and not
    // including) the first (
    TQString name = item->text(0).section('(', 0, 0);

    FunctionManager::instance()->removeFunction(name);
}

void FunctionListView::removeAllItems()
{
    TQStringList fns = FunctionManager::instance()->functionList(FunctionManager::UserDefined);

    for(TQStringList::ConstIterator fn = fns.constBegin(); fn != fns.constEnd(); ++fn)
       FunctionManager::instance()->removeFunction(*fn);
}

#include "abakuslistview.moc"