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 <tdeconfig.h>
35#include <tdeglobalsettings.h>
36#include <tdelocale.h>
37
38#include <kexidb/tableschema.h>
39#include <kexidb/queryschema.h>
40#include <kexidb/utils.h>
41#include <kexidragobjects.h>
42#include <kexiutils/utils.h>
43
44KexiFieldListView::KexiFieldListView(TQWidget *parent, const char *name, int options)
45 : TDEListView(parent, name)
46 , m_schema(0)
47 , m_keyIcon(SmallIcon("key"))
48 , m_noIcon(KexiUtils::emptyIcon(TDEIcon::Small))
49 , m_options(options)
50 , m_allColumnsItem(0)
51{
52 setAcceptDrops(true);
53 viewport()->setAcceptDrops(true);
54 setDropVisualizer(false);
55 setDropHighlighter(true);
56 setAllColumnsShowFocus(true);
57 addColumn(i18n("Field Name"));
58 if (m_options & ShowDataTypes)
59 addColumn(i18n("Data Type"));
60 if (m_options & AllowMultiSelection)
61 setSelectionMode(TQListView::Extended);
62 setResizeMode(TQListView::LastColumn);
63// header()->hide();
64 setSorting(-1, true); // disable sorting
65 setDragEnabled(true);
66
67 connect(this, TQ_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint &, int)),
68 this, TQ_SLOT(slotDoubleClicked(TQListViewItem*)));
69}
70
71KexiFieldListView::~KexiFieldListView()
72{
73 delete m_schema;
74}
75
77{
78 if (schema && m_schema == schema)
79 return;
80 m_allColumnsItem = 0;
81 clear();
82 delete m_schema;
83 m_schema = schema;
84 if (!m_schema)
85 return;
86
87 int order=0;
88 bool hasPKeys = true; //t->hasPrimaryKeys();
89 TDEListViewItem *item = 0;
90 KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true /*unique*/);
91 const int count = columns.count();
92 for(int i=-1; i < count; i++)
93 {
94 KexiDB::QueryColumnInfo *colinfo = 0;
95 if (i==-1) {
96 if (! (m_options & ShowAsterisk))
97 continue;
98 item = new TDEListViewItem(this, item, i18n("* (All Columns)"));
99 m_allColumnsItem = item;
100 }
101 else {
102 colinfo = columns[i];
103 item = new TDEListViewItem(this, item, colinfo->aliasOrName());
104 if (m_options & ShowDataTypes)
105 item->setText(1, colinfo->field->typeName());
106 }
107 if(colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
108 item->setPixmap(0, m_keyIcon);
109 else if (hasPKeys) {
110 item->setPixmap(0, m_noIcon);
111 }
112 order++;
113 }
114
115 setCurrentItem(firstChild());
116}
117
118#if 0
119TQSize KexiFieldListView::sizeHint()
120{
121 TQFontMetrics fm(font());
122
123 kdDebug() << m_table->name() << " cw=" << columnWidth(1) + fm.width("i") << ", " << fm.width(m_table->name()+" ") << endl;
124
125 TQSize s(
126 TQMAX( columnWidth(1) + fm.width("i"), fm.width(m_table->name()+" ")),
127 childCount()*firstChild()->totalHeight() + 4 );
128// TQSize s( columnWidth(1), childCount()*firstChild()->totalHeight() + 3*firstChild()->totalHeight()/10);
129 return s;
130}
131
132void KexiFieldListView::setReadOnly(bool b)
133{
134 setAcceptDrops(!b);
135 viewport()->setAcceptDrops(!b);
136}
137#endif
138
139TQDragObject* KexiFieldListView::dragObject()
140{
141 if (!schema())
142 return 0;
143 const TQStringList selectedFields( selectedFieldNames() );
144 return new KexiFieldDrag(m_schema->table() ? "kexi/table" : "kexi/query",
145 m_schema->name(), selectedFields, this, "KexiFieldDrag");
146/* if (selectedItem()) {
147 KexiFieldDrag *drag = new KexiFieldDrag("kexi/table", m_schema->name(),
148 selectedItem()->text(1), this, "KexiFieldDrag");
149 return drag;
150 }*/
151}
152
154{
155 if (!schema())
156 return TQStringList();
157 TQStringList selectedFields;
158 for (TQListViewItem *item = firstChild(); item; item = item->nextSibling()) {
159 if (item->isSelected()) {
161 if (item == m_allColumnsItem && m_allColumnsItem)
162 selectedFields.append("*");
163 else
164 selectedFields.append(item->text(0));
165 }
166 }
167 return selectedFields;
168}
169
170void KexiFieldListView::slotDoubleClicked(TQListViewItem* item)
171{
172 if (schema() && item) {
174 emit fieldDoubleClicked(schema()->table() ? "kexi/table" : "kexi/query",
175 schema()->name(), item->text(0));
176 }
177}
178
179#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
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)