kexi

kexifieldlistview.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 //  header()->hide();
00067     setSorting(-1, true); // disable sorting
00068     setDragEnabled(true);
00069 }
00070 
00071 KexiFieldListView::~KexiFieldListView()
00072 {
00073     delete m_schema;
00074 }
00075 
00076 void KexiFieldListView::setSchema(KexiDB::TableOrQuerySchema* schema)
00077 {
00078     if (schema && m_schema == schema)
00079         return;
00080     clear();
00081     delete m_schema;
00082     m_schema = schema;
00083     if (!m_schema)
00084         return;
00085 
00086     int order=0;
00087     bool hasPKeys = true; //t->hasPrimaryKeys();
00088     KListViewItem *item = 0;
00089     KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true /*unique*/);
00090     const int count = columns.count();
00091     for(int i=-1; i < count; i++)
00092     {
00093         KexiDB::QueryColumnInfo *colinfo = 0;
00094         if (i==-1) {
00095             if (! (m_options & ShowAsterisk))
00096                 continue;
00097             item = new KListViewItem(this, item, "*");
00098         }
00099         else {
00100             colinfo = columns[i];
00101             item = new KListViewItem(this, item, colinfo->aliasOrName());
00102             if (m_options & ShowDataTypes)
00103                 item->setText(1, colinfo->field->typeName());
00104         }
00105         if(colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
00106             item->setPixmap(0, m_keyIcon);
00107         else if (hasPKeys) {
00108             item->setPixmap(0, m_noIcon);
00109         }
00110         order++;
00111     }
00112 
00113     setCurrentItem(firstChild());
00114 }
00115 
00116 #if 0
00117 QSize KexiFieldListView::sizeHint()
00118 {
00119     QFontMetrics fm(font());
00120 
00121     kdDebug() << m_table->name() << " cw=" << columnWidth(1) + fm.width("i") << ", " << fm.width(m_table->name()+"  ") << endl; 
00122 
00123     QSize s( 
00124         QMAX( columnWidth(1) + fm.width("i"), fm.width(m_table->name()+"  ")), 
00125         childCount()*firstChild()->totalHeight() + 4 );
00126 //  QSize s( columnWidth(1), childCount()*firstChild()->totalHeight() + 3*firstChild()->totalHeight()/10);
00127     return s;
00128 }
00129 
00130 void KexiFieldListView::setReadOnly(bool b)
00131 {
00132     setAcceptDrops(!b);
00133     viewport()->setAcceptDrops(!b);
00134 }
00135 #endif
00136 
00137 QDragObject* KexiFieldListView::dragObject()
00138 {
00139     QStringList selectedFields;
00140     for (QListViewItemIterator it(this); it.current(); ++it) {
00141         if (it.current()->isSelected()) {
00143             selectedFields.append(it.current()->text(0));
00144         }
00145     }
00146     return new KexiFieldDrag(m_schema->table() ? "kexi/table" : "kexi/query", 
00147         m_schema->name(), selectedFields, this, "KexiFieldDrag");
00148 /*  if (selectedItem()) {
00149         KexiFieldDrag *drag = new KexiFieldDrag("kexi/table", m_schema->name(), 
00150             selectedItem()->text(1), this, "KexiFieldDrag");
00151             return drag;
00152     }*/
00153 }
00154 
00155 #include "kexifieldlistview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys