kexi

kexifieldlistview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexifieldlistview.h"
21
22#include <tqheader.h>
23#include <tqlayout.h>
24#include <tqlabel.h>
25#include <tqpushbutton.h>
26#include <tqcursor.h>
27#include <tqpoint.h>
28#include <tqapplication.h>
29#include <tqbitmap.h>
30#include <tqstyle.h>
31
32#include <kdebug.h>
33#include <kiconloader.h>
34#include <tdeversion.h>
35#include <tdeconfig.h>
36#include <tdeglobalsettings.h>
37#include <tdelocale.h>
38
39#include <kexidb/tableschema.h>
40#include <kexidb/queryschema.h>
41#include <kexidb/utils.h>
42#include <kexidragobjects.h>
43#include <kexiutils/utils.h>
44
45KexiFieldListView::KexiFieldListView(TQWidget *parent, const char *name, int options)
46 : TDEListView(parent, name)
47 , m_schema(0)
48 , m_keyIcon(SmallIcon("key"))
49 , m_noIcon(KexiUtils::emptyIcon(TDEIcon::Small))
50 , m_options(options)
51 , m_allColumnsItem(0)
52{
53 setAcceptDrops(true);
54 viewport()->setAcceptDrops(true);
55 setDropVisualizer(false);
56 setDropHighlighter(true);
57 setAllColumnsShowFocus(true);
58 addColumn(i18n("Field Name"));
59 if (m_options & ShowDataTypes)
60 addColumn(i18n("Data Type"));
61 if (m_options & AllowMultiSelection)
62 setSelectionMode(TQListView::Extended);
63 setResizeMode(TQListView::LastColumn);
64// header()->hide();
65 setSorting(-1, true); // disable sorting
66 setDragEnabled(true);
67
68 connect(this, TQ_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint &, int)),
69 this, TQ_SLOT(slotDoubleClicked(TQListViewItem*)));
70}
71
72KexiFieldListView::~KexiFieldListView()
73{
74 delete m_schema;
75}
76
78{
79 if (schema && m_schema == schema)
80 return;
81 m_allColumnsItem = 0;
82 clear();
83 delete m_schema;
84 m_schema = schema;
85 if (!m_schema)
86 return;
87
88 int order=0;
89 bool hasPKeys = true; //t->hasPrimaryKeys();
90 TDEListViewItem *item = 0;
91 KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true /*unique*/);
92 const int count = columns.count();
93 for(int i=-1; i < count; i++)
94 {
95 KexiDB::QueryColumnInfo *colinfo = 0;
96 if (i==-1) {
97 if (! (m_options & ShowAsterisk))
98 continue;
99 item = new TDEListViewItem(this, item, i18n("* (All Columns)"));
100 m_allColumnsItem = item;
101 }
102 else {
103 colinfo = columns[i];
104 item = new TDEListViewItem(this, item, colinfo->aliasOrName());
105 if (m_options & ShowDataTypes)
106 item->setText(1, colinfo->field->typeName());
107 }
108 if(colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
109 item->setPixmap(0, m_keyIcon);
110 else if (hasPKeys) {
111 item->setPixmap(0, m_noIcon);
112 }
113 order++;
114 }
115
116 setCurrentItem(firstChild());
117}
118
119#if 0
120TQSize KexiFieldListView::sizeHint()
121{
122 TQFontMetrics fm(font());
123
124 kdDebug() << m_table->name() << " cw=" << columnWidth(1) + fm.width("i") << ", " << fm.width(m_table->name()+" ") << endl;
125
126 TQSize s(
127 TQMAX( columnWidth(1) + fm.width("i"), fm.width(m_table->name()+" ")),
128 childCount()*firstChild()->totalHeight() + 4 );
129// TQSize s( columnWidth(1), childCount()*firstChild()->totalHeight() + 3*firstChild()->totalHeight()/10);
130 return s;
131}
132
133void KexiFieldListView::setReadOnly(bool b)
134{
135 setAcceptDrops(!b);
136 viewport()->setAcceptDrops(!b);
137}
138#endif
139
140TQDragObject* KexiFieldListView::dragObject()
141{
142 if (!schema())
143 return 0;
144 const TQStringList selectedFields( selectedFieldNames() );
145 return new KexiFieldDrag(m_schema->table() ? "kexi/table" : "kexi/query",
146 m_schema->name(), selectedFields, this, "KexiFieldDrag");
147/* if (selectedItem()) {
148 KexiFieldDrag *drag = new KexiFieldDrag("kexi/table", m_schema->name(),
149 selectedItem()->text(1), this, "KexiFieldDrag");
150 return drag;
151 }*/
152}
153
155{
156 if (!schema())
157 return TQStringList();
158 TQStringList selectedFields;
159 for (TQListViewItem *item = firstChild(); item; item = item->nextSibling()) {
160 if (item->isSelected()) {
162 if (item == m_allColumnsItem && m_allColumnsItem)
163 selectedFields.append("*");
164 else
165 selectedFields.append(item->text(0));
166 }
167 }
168 return selectedFields;
169}
170
171void KexiFieldListView::slotDoubleClicked(TQListViewItem* item)
172{
173 if (schema() && item) {
175 emit fieldDoubleClicked(schema()->table() ? "kexi/table" : "kexi/query",
176 schema()->name(), item->text(0));
177 }
178}
179
180#include "kexifieldlistview.moc"
bool isPrimaryKey() const
Definition field.h:234
static TQString typeName(uint type)
Definition field.cpp:173
bool isUniqueKey() const
Definition field.h:237
Helper class that assigns additional information for the column in a query.
Definition queryschema.h:49
TQCString aliasOrName() const
Definition queryschema.h:59
const QueryColumnInfo::Vector columns(bool unique=false)
TableSchema * table() const
Drag object containing information about field(s).
TQPixmap m_keyIcon
a small "primary key" icon for 0-th column
void fieldDoubleClicked(const TQString &sourceMimeType, const TQString &sourceName, const TQString &fieldName)
TQPixmap m_noIcon
blank icon of the same size as m_keyIcon
KexiDB::TableOrQuerySchema * schema() const
@ ShowDataTypes
if set, 'data type' column is added
@ ShowAsterisk
if set, asterisk ('*') item is prepended to the list
void slotDoubleClicked(TQListViewItem *item)
void setSchema(KexiDB::TableOrQuerySchema *schema)
TQStringList selectedFieldNames() const
General Utils.
Definition identifier.h:27
KEXIUTILS_EXPORT TQPixmap emptyIcon(TDEIcon::Group iconGroup)