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 #include <kexiutils/utils.h>
00044
00045 KexiFieldListView::KexiFieldListView(QWidget *parent, const char *name, int options)
00046 : KListView(parent, name)
00047 , m_schema(0)
00048 , m_keyIcon(SmallIcon("key"))
00049 , m_noIcon(KexiUtils::emptyIcon(KIcon::Small))
00050 , m_options(options)
00051 , m_allColumnsItem(0)
00052 {
00053 setAcceptDrops(true);
00054 viewport()->setAcceptDrops(true);
00055 setDropVisualizer(false);
00056 setDropHighlighter(true);
00057 setAllColumnsShowFocus(true);
00058 addColumn(i18n("Field Name"));
00059 if (m_options & ShowDataTypes)
00060 addColumn(i18n("Data Type"));
00061 if (m_options & AllowMultiSelection)
00062 setSelectionMode(QListView::Extended);
00063 setResizeMode(QListView::LastColumn);
00064
00065 setSorting(-1, true);
00066 setDragEnabled(true);
00067
00068 connect(this, SIGNAL(doubleClicked(QListViewItem*, const QPoint &, int)),
00069 this, SLOT(slotDoubleClicked(QListViewItem*)));
00070 }
00071
00072 KexiFieldListView::~KexiFieldListView()
00073 {
00074 delete m_schema;
00075 }
00076
00077 void KexiFieldListView::setSchema(KexiDB::TableOrQuerySchema* schema)
00078 {
00079 if (schema && m_schema == schema)
00080 return;
00081 m_allColumnsItem = 0;
00082 clear();
00083 delete m_schema;
00084 m_schema = schema;
00085 if (!m_schema)
00086 return;
00087
00088 int order=0;
00089 bool hasPKeys = true;
00090 KListViewItem *item = 0;
00091 KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true );
00092 const int count = columns.count();
00093 for(int i=-1; i < count; i++)
00094 {
00095 KexiDB::QueryColumnInfo *colinfo = 0;
00096 if (i==-1) {
00097 if (! (m_options & ShowAsterisk))
00098 continue;
00099 item = new KListViewItem(this, item, i18n("* (All Columns)"));
00100 m_allColumnsItem = 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 if (!schema())
00143 return 0;
00144 const QStringList selectedFields( selectedFieldNames() );
00145 return new KexiFieldDrag(m_schema->table() ? "kexi/table" : "kexi/query",
00146 m_schema->name(), selectedFields, this, "KexiFieldDrag");
00147
00148
00149
00150
00151
00152 }
00153
00154 QStringList KexiFieldListView::selectedFieldNames() const
00155 {
00156 if (!schema())
00157 return QStringList();
00158 QStringList selectedFields;
00159 for (QListViewItem *item = firstChild(); item; item = item->nextSibling()) {
00160 if (item->isSelected()) {
00162 if (item == m_allColumnsItem && m_allColumnsItem)
00163 selectedFields.append("*");
00164 else
00165 selectedFields.append(item->text(0));
00166 }
00167 }
00168 return selectedFields;
00169 }
00170
00171 void KexiFieldListView::slotDoubleClicked(QListViewItem* item)
00172 {
00173 if (schema() && item) {
00175 emit fieldDoubleClicked(schema()->table() ? "kexi/table" : "kexi/query",
00176 schema()->name(), item->text(0));
00177 }
00178 }
00179
00180 #include "kexifieldlistview.moc"
|