aboutsummaryrefslogtreecommitdiffstats
path: root/stack.cpp
blob: 099cfd63da6ff5ad173d78933e76f1f02bb0aadc (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
/****************************************************************************
 *                                                                          *
 *   Copyright (C) 2005  Michael Buesch <mbuesch@freenet.de>                *
 *                                                                          *
 *   This program is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License version 2         *
 *   as published by the Free Software Foundation.                          *
 *                                                                          *
 *   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. Additionally a copy of the                    *
 *   GNU General Public License is available here:                          *
 *   http://passwordmanager.sourceforge.net/gplv2.txt                       *
 *   http://www.gnu.org/licenses/gpl.txt                                    *
 *                                                                          *
 ****************************************************************************/

#include "regtrack.h"
#include "stack.h"

#define OFFSET_COLUMN	0
#define DATA_COLUMN	1

/***************************************************************************
 *  class StackItem
 ***************************************************************************/

StackItem::StackItem(StackView *_parent, int _pos)
 : QListViewItem (_parent)
 , parent (_parent)
 , pos (_pos)
{
	setRenameEnabled(DATA_COLUMN, true);
}

StackItem::~StackItem()
{
}

void StackItem::setName(const QString &str)
{
	QListViewItem::setText(OFFSET_COLUMN, str);
}

void StackItem::setText(const QString &str)
{
	QListViewItem::setText(DATA_COLUMN, str);
}

QString StackItem::text() const
{
	return QListViewItem::text(DATA_COLUMN);
}

int StackItem::width(const QFontMetrics &fm,
		     const QListView *lv,
		     int c) const
{
	return QListViewItem::width(fm, lv, c) + 10;
}

int StackItem::compare(QListViewItem *_item,
		       int /*col*/,
		       bool /*ascending*/) const
{
	StackItem *item = dynamic_cast<StackItem *>(_item);
	if (!item)
		return 0;
	return pos - item->pos;
}


/***************************************************************************
 *  class StackView
 ***************************************************************************/

StackView::StackView(RegTrack *_rt,
		     const char *name,
		     WFlags f)
 : QListView (_rt, name, f)
 , rt (_rt)
{
	setFont(RegTrack::dataFont);
	addColumn("Offset");
	addColumn("Data", -1);
	reset();
	setDefaultRenameAction(Accept);
	connect(this, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)),
		this, SLOT(itemDoubleClicked(QListViewItem *, const QPoint &, int)));
	connect(this, SIGNAL(mouseButtonClicked(int, QListViewItem *, const QPoint &, int)),
		this, SLOT(itemClicked(int, QListViewItem *, const QPoint &, int)));
}

StackView::~StackView()
{
}

int StackView::size() const
{
	return childCount() * sizeof(int32_t);
}

int StackView::count() const
{
	return childCount();
}

void StackView::push(QString str)
{
	if (str.isEmpty())
		str = "<undefined>";
	StackItem *item = new StackItem(this, list.count());
	list.append(item);
	item->setText(str);
	updateItemNames();
	adjustColumn(DATA_COLUMN);
	if (rt->freeSize && list.size() > 1)
		rt->freeSize->setMaxValue((list.size() - 1) * sizeof(int32_t));
}

QString StackView::pop()
{
	if (list.isEmpty())
		return QString();
	StackItem *item = list.last();
	QString ret = item->text();
	if (count() > 1) {
		delete item;
		list.pop_back();
	}
	updateItemNames();
	if (rt->freeSize && list.size() > 1)
		rt->freeSize->setMaxValue((list.size() - 1) * sizeof(int32_t));
	return ret;
}

void StackView::updateItemNames()
{
	int cnt = list.size() - 1;
	QString str;
	StackItem *item;
	QValueList<StackItem *>::iterator i = list.begin(),
					  end = list.end();
	for ( ; i != end; ++i) {
		item = *i;
		str = QString::number(cnt * sizeof(int32_t), 16);
		cnt--;
		str += "(%esp)";
		item->setName(str);
	}
}

void StackView::reset(bool full)
{
	QValueList<StackItem *>::iterator i = list.begin(),
					  end = list.end();
	for ( ; i != end; ++i)
		delete *i;
	list.clear();
	if (!full)
		push("stack bottom");
}

void StackView::itemDoubleClicked(QListViewItem *_item,
				  const QPoint &/*point*/,
				  int /*column*/)
{
	StackItem *item = dynamic_cast<StackItem *>(_item);
	if (!item)
		return;
	item->startRename(DATA_COLUMN);
}

void StackView::itemClicked(int button,
			    QListViewItem *_item,
			    const QPoint &/*pos*/,
			    int /*column*/)
{
	StackItem *item = dynamic_cast<StackItem *>(_item);
	if (!item)
		return;
	if (button == Qt::MidButton)
		rt->clipboardPut(item->text());
}

void StackView::load(QDataStream &ds)
{
	QString str;
	int cnt;

	reset(true);
	ds >> cnt;
	for ( ; cnt; cnt--) {
		ds >> str;
		push(str);
	}
}

void StackView::save(QDataStream &ds)
{
	ds << count();
	QValueList<StackItem *>::iterator i = list.begin(),
					  end = list.end();
	for ( ; i != end; ++i)
		ds << (*i)->text();
}
bues.ch cgit interface