summaryrefslogtreecommitdiffstats
path: root/src/resultlistview.cpp
blob: 86db28f270e1f8c9930b83b2c17979c3ce81df3b (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
/*
 * resultlistview.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 <kdebug.h>
#include <tdepopupmenu.h>
#include <tdelocale.h>

#include <tqclipboard.h>
#include <tqapplication.h>
#include <tqevent.h>
#include <tqcursor.h>
#include <tqdragobject.h>
#include <tqheader.h>

#include "resultlistview.h"
#include "resultlistviewtext.h"
#include "dragsupport.h"

using DragSupport::makePixmap;
using namespace ResultList;

ResultListView::ResultListView(TQWidget *parent, const char *name) :
    TDEListView(parent, name), m_itemRightClicked(0)
{
    connect(this, TQT_SIGNAL(doubleClicked(TQListViewItem *, const TQPoint &, int)),
                  TQT_SLOT(slotDoubleClicked(TQListViewItem *, const TQPoint &, int)));

    addColumn(i18n("Expression"));
    addColumn(i18n("Result"));
    addColumn(i18n("Shortcut"));

    header()->hide(); // I hate that header
    header()->setStretchEnabled(ResultColumn, true);

    setDragEnabled(true);
    setItemMargin(2);
    setColumnAlignment(ResultColumn, AlignLeft);
    setColumnAlignment(ShortcutColumn, AlignHCenter);
    setColumnWidthMode(ResultColumn, Maximum);
    setSortColumn(-1);
}

bool ResultListView::getStackValue(unsigned stackPosition, Abakus::number_t &result)
{
    TQListViewItem *it = firstChild();
    for(; it; it = it->itemBelow()) {
	ResultListViewText *resultItem = dynamic_cast<ResultListViewText *>(it);
	if(!resultItem->wasError() && resultItem->stackPosition() == stackPosition) {
	    result = Abakus::number_t(resultItem->resultText().latin1());
	    return true;
	}
    }

    return false;
}

TQDragObject *ResultListView::dragObject()
{
    TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
    ResultListViewText *item = itemUnderCursor();

    if(item) {
	TQString text = item->resultText();

	int column = header()->sectionAt(viewportPos.x());

	if(column == ExpressionColumn)
	    text = item->expressionText();

	TQDragObject *drag = new TQTextDrag(text, this);
	drag->setPixmap(makePixmap(text, font()));

	return drag;
    }

    return 0;
}

void ResultListView::contextMenuEvent(TQContextMenuEvent *e)
{
    m_itemRightClicked = itemUnderCursor();
    TDEPopupMenu *menu = constructPopupMenu(m_itemRightClicked);

    menu->popup(e->globalPos());
}

void ResultListView::slotDoubleClicked(TQListViewItem *item, const TQPoint &, int c)
{
    ResultListViewText *textItem = dynamic_cast<ResultListViewText *>(item);
    if(!textItem)
	return;

    if(c == ExpressionColumn)
	emit signalEntrySelected(textItem->expressionText());
    else if(c == ResultColumn)
	emit signalResultSelected(textItem->resultText());
}

TDEPopupMenu *ResultListView::constructPopupMenu(const ResultListViewText *item)
{
    TDEPopupMenu *menu = new TDEPopupMenu(this, "list view context menu");

    menu->insertItem(i18n("Clear &History"), this, TQT_SLOT(clear()), ALT+Key_R);

    int id = menu->insertItem(i18n("Copy Result to Clipboard"), this, TQT_SLOT(slotCopyResult()));
    if(!item || item->wasError())
	menu->setItemEnabled(id, false);

    return menu;
}

void ResultListView::slotCopyResult()
{
    if(!m_itemRightClicked)
	return;

    TQClipboard *clipboard = TQApplication::clipboard();

    clipboard->setText(m_itemRightClicked->resultText(), TQClipboard::Clipboard);
}

ResultListViewText *ResultListView::lastItem() const
{
    return static_cast<ResultListViewText *>(TDEListView::lastItem());
}

ResultListViewText *ResultListView::itemUnderCursor() const
{
    TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
    TQListViewItem *underCursor = itemAt(viewportPos);
    return static_cast<ResultListViewText *>(underCursor);
}

#include "resultlistview.moc"