kexi
kexifieldlistview.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kexifieldlistview.h"
00021
00022 #include <qheader.h>
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qpushbutton.h>
00026 #include <qcursor.h>
00027 #include <qpoint.h>
00028 #include <qapplication.h>
00029 #include <qbitmap.h>
00030 #include <qstyle.h>
00031
00032 #include <kdebug.h>
00033 #include <kiconloader.h>
00034 #include <kdeversion.h>
00035 #include <kconfig.h>
00036 #include <kglobalsettings.h>
00037 #include <klocale.h>
00038
00039 #include <kexidb/tableschema.h>
00040 #include <kexidb/queryschema.h>
00041 #include <kexidb/utils.h>
00042 #include <kexidragobjects.h>
00043
00044 KexiFieldListView::KexiFieldListView(QWidget *parent, const char *name, int options)
00045 : KListView(parent, name)
00046 , m_schema(0)
00047 , m_options(options)
00048 {
00049 m_keyIcon = SmallIcon("key");
00050 m_noIcon = QPixmap(m_keyIcon.size());
00051 QBitmap bmp(m_noIcon.size());
00052 bmp.fill(Qt::color0);
00053 m_noIcon.setMask(bmp);
00054
00055 setAcceptDrops(true);
00056 viewport()->setAcceptDrops(true);
00057 setDropVisualizer(false);
00058 setDropHighlighter(true);
00059 setAllColumnsShowFocus(true);
00060 addColumn(i18n("Field Name"));
00061 if (m_options & ShowDataTypes)
00062 addColumn(i18n("Data Type"));
00063 if (m_options & AllowMultiSelection)
00064 setSelectionMode(QListView::Extended);
00065 setResizeMode(QListView::LastColumn);
00066
00067 setSorting(-1, true);
00068 setDragEnabled(true);
00069
00070 connect(this, SIGNAL(doubleClicked(QListViewItem*, const QPoint &, int)),
00071 this, SLOT(slotDoubleClicked(QListViewItem*)));
00072 }
00073
00074 KexiFieldListView::~KexiFieldListView()
00075 {
00076 delete m_schema;
00077 }
00078
00079 void KexiFieldListView::setSchema(KexiDB::TableOrQuerySchema* schema)
00080 {
00081 if (schema && m_schema == schema)
00082 return;
00083 clear();
00084 delete m_schema;
00085 m_schema = schema;
00086 if (!m_schema)
00087 return;
00088
00089 int order=0;
00090 bool hasPKeys = true;
00091 KListViewItem *item = 0;
00092 KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true );
00093 const int count = columns.count();
00094 for(int i=-1; i < count; i++)
00095 {
00096 KexiDB::QueryColumnInfo *colinfo = 0;
00097 if (i==-1) {
00098 if (! (m_options & ShowAsterisk))
00099 continue;
00100 item = new KListViewItem(this, item, "*");
00101 }
00102 else {
00103 colinfo = columns[i];
00104 item = new KListViewItem(this, item, colinfo->aliasOrName());
00105 if (m_options & ShowDataTypes)
00106 item->setText(1, colinfo->field->typeName());
00107 }
00108 if(colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
00109 item->setPixmap(0, m_keyIcon);
00110 else if (hasPKeys) {
00111 item->setPixmap(0, m_noIcon);
00112 }
00113 order++;
00114 }
00115
00116 setCurrentItem(firstChild());
00117 }
00118
00119 #if 0
00120 QSize KexiFieldListView::sizeHint()
00121 {
00122 QFontMetrics fm(font());
00123
00124 kdDebug() << m_table->name() << " cw=" << columnWidth(1) + fm.width("i") << ", " << fm.width(m_table->name()+" ") << endl;
00125
00126 QSize s(
00127 QMAX( columnWidth(1) + fm.width("i"), fm.width(m_table->name()+" ")),
00128 childCount()*firstChild()->totalHeight() + 4 );
00129
00130 return s;
00131 }
00132
00133 void KexiFieldListView::setReadOnly(bool b)
00134 {
00135 setAcceptDrops(!b);
00136 viewport()->setAcceptDrops(!b);
00137 }
00138 #endif
00139
00140 QDragObject* KexiFieldListView::dragObject()
00141 {
00142 QStringList selectedFields;
00143 for (QListViewItemIterator it(this); it.current(); ++it) {
00144 if (it.current()->isSelected()) {
00146 selectedFields.append(it.current()->text(0));
00147 }
00148 }
00149 return new KexiFieldDrag(m_schema->table() ? "kexi/table" : "kexi/query",
00150 m_schema->name(), selectedFields, this, "KexiFieldDrag");
00151
00152
00153
00154
00155
00156 }
00157
00158 QStringList KexiFieldListView::selectedFieldNames()
00159 {
00160 if (!schema())
00161 return QStringList();
00162 QStringList selectedFields;
00163 for (QListViewItemIterator it(this); it.current(); ++it) {
00164 if (it.current()->isSelected()) {
00166 selectedFields.append(it.current()->text(0));
00167 }
00168 }
00169 return selectedFields;
00170 }
00171
00172 void KexiFieldListView::slotDoubleClicked(QListViewItem* item)
00173 {
00174 if (schema() && item) {
00176 emit fieldDoubleClicked(schema()->table() ? "kexi/table" : "kexi/query",
00177 schema()->name(), item->text(0));
00178 }
00179 }
00180
00181 #include "kexifieldlistview.moc"
|