/**************************************************************************** * * * Copyright (C) 2005 Michael Buesch * * * * 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(_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 = ""; 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::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::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(_item); if (!item) return; item->startRename(DATA_COLUMN); } void StackView::itemClicked(int button, QListViewItem *_item, const QPoint &/*pos*/, int /*column*/) { StackItem *item = dynamic_cast(_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::iterator i = list.begin(), end = list.end(); for ( ; i != end; ++i) ds << (*i)->text(); }