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 #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 //  header()->hide();
00065     setSorting(-1, true); // disable sorting
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; //t->hasPrimaryKeys();
00090     KListViewItem *item = 0;
00091     KexiDB::QueryColumnInfo::Vector columns = m_schema->columns(true /*unique*/);
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 //  QSize s( columnWidth(1), childCount()*firstChild()->totalHeight() + 3*firstChild()->totalHeight()/10);
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 /*  if (selectedItem()) {
00148         KexiFieldDrag *drag = new KexiFieldDrag("kexi/table", m_schema->name(), 
00149             selectedItem()->text(1), this, "KexiFieldDrag");
00150             return drag;
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"
KDE Home | KDE Accessibility Home | Description of Access Keys