File: abakuslistview.cpp

package info (click to toggle)
abakus 0.91-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 944 kB
  • ctags: 1,245
  • sloc: cpp: 5,498; ansic: 1,414; python: 1,272; yacc: 236; lex: 205; makefile: 27
file content (232 lines) | stat: -rw-r--r-- 5,816 bytes parent folder | download
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <klocale.h>
#include <kpopupmenu.h>
#include <kdebug.h>

#include <qdragobject.h>
#include <qcursor.h>
#include <qheader.h>

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

ListView::ListView(QWidget *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, SIGNAL(contextMenuRequested(QListViewItem *, const QPoint &, int)),
	          SLOT(rightClicked(QListViewItem *, const QPoint &)));
}

QDragObject *ListView::dragObject()
{
    QPoint viewportPos = viewport()->mapFromGlobal(QCursor::pos());
    QListViewItem *item = itemAt(viewportPos);

    if(!item)
	return 0;

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

    QDragObject *drag = new QTextDrag(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, SLOT(removeSelected()));
	m_removeAllId = m_menu->insertItem("Placeholder", this, SLOT(removeAllItems()));
    }
    else {
	delete m_menu;
	m_menu = 0;
    }
}

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

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

    return QString();
}

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

void ListView::removeAllItems()
{
}

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

    return false;
}

void ListView::rightClicked(QListViewItem *item, const QPoint &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(QListView *listView, const QString &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(QWidget *parent, const char *name) :
    ListView(parent, name)
{
    enablePopupHandler(true);
}

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

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

    for(QStringList::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(QListViewItem *item) const
{
    return !ValueManager::instance()->isValueReadOnly(item->text(0));
}

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

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

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

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

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

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

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

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

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

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

#include "abakuslistview.moc"