Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omglistwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 Tim Sutton   *
00003  *   tim@linfiniti.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 //local includes
00022 #include "omglistwidget.h"
00023 //qt includes
00024 #include <QListWidget>
00025 #include <QListWidgetItem>
00026 #include <QStringList>
00027 #include <QListWidgetItem>
00028 
00029   OmgListWidget::OmgListWidget(QWidget* parent, Qt::WFlags fl)
00030 :  QWidget(parent,fl)
00031 {
00032   //required by Qt4 to initialise the ui
00033   setupUi(this);
00034   lstItems->setEnabled(true);
00035   //we want this widget to behave list a QListWidget, so item clicked events in 
00036   //the internal list must be propogated
00037   connect (lstItems,SIGNAL(itemPressed(QListWidgetItem *)),
00038                   this,SLOT(itemPressedSlot(QListWidgetItem *)));
00039    
00040 }
00041 
00042 OmgListWidget::~OmgListWidget()
00043 {}
00044 
00045 //this is a private slot whose only job is to propogate teh 
00046 //itemClick event of the lstItems internal widget out beyond
00047 //this custom widget
00048 void OmgListWidget::itemPressedSlot(QListWidgetItem * thepItem)
00049 {
00050   emit itemPressed(thepItem);
00051   /* Commented out for now since it causes a conflict
00052    * with checkbox toggle behaviour. Clients of this
00053    * class can still use the signal itemPressed if they want to
00054 
00055   if (thepItem->checkState()==Qt::Checked)
00056   {
00057     thepItem->setCheckState(Qt::Unchecked);
00058   }
00059   else
00060   {
00061     thepItem->setCheckState(Qt::Checked);
00062   }
00063   qDebug ("Item pressed: " + thepItem->text().toLocal8Bit());
00064   */
00065 }
00066 
00067 void OmgListWidget::selectAll()
00068 {
00069   qDebug("OmgListWidget::selectAll");
00070   for ( int myCounter = 0; myCounter < lstItems->count(); myCounter++ )
00071   {
00072     QListWidgetItem *  mypItem = lstItems->item(myCounter);
00073     mypItem->setCheckState(Qt::Checked);
00074     qDebug(mypItem->text().toLocal8Bit() + " has been checked");
00075   }
00076 }
00077 
00078 void OmgListWidget::invertSelection()
00079 {
00080   qDebug("OmgListWidget::invertSelection");
00081   for ( int myCounter = 0; myCounter < lstItems->count(); myCounter++ )
00082   {
00083     QListWidgetItem *  mypItem = lstItems->item(myCounter);
00084     if (mypItem->checkState()==Qt::Checked)
00085     {
00086       mypItem->setCheckState(Qt::Unchecked);
00087       qDebug(mypItem->text().toLocal8Bit() + " has been unchecked");
00088     }
00089     else
00090     {
00091       mypItem->setCheckState(Qt::Checked);
00092       qDebug(mypItem->text().toLocal8Bit() + " has been checked");
00093     }
00094   }
00095 }
00096 
00097 void OmgListWidget::selectNone()
00098 {
00099   qDebug("OmgListWidget::selectNone");
00100   for ( int myCounter = 0; myCounter < lstItems->count(); myCounter++ )
00101   {
00102     QListWidgetItem *  mypItem = lstItems->item(myCounter);
00103     mypItem->setCheckState(Qt::Unchecked);
00104     qDebug(mypItem->text().toLocal8Bit() + " has been unchecked");
00105   }
00106 }
00107 
00108 int OmgListWidget::checkedItemCount()
00109 {
00110   return checkedItems().count();
00111 }
00112 
00113 QStringList OmgListWidget::checkedItems()
00114 {
00115   QStringList myList;
00116   for ( int myCounter = 0; myCounter < lstItems->count(); myCounter++ )
00117   {
00118     QListWidgetItem *  mypItem = lstItems->item(myCounter);
00119     if (mypItem->checkState()==Qt::Checked)
00120     {
00121       myList << mypItem->text();
00122     }
00123   }
00124   return myList;
00125 }
00126 QStringList OmgListWidget::checkedDataItems()
00127 {
00128   QStringList myList;
00129   for ( int myCounter = 0; myCounter < lstItems->count(); myCounter++ )
00130   {
00131     QListWidgetItem *  mypItem = lstItems->item(myCounter);
00132     if (mypItem->checkState()==Qt::Checked)
00133     {
00134       myList << mypItem->data(Qt::UserRole).toString();
00135     }
00136   }
00137   return myList;
00138 }
00139 void OmgListWidget::hideOptionsTool()
00140 {
00141   toolConfigure->hide();
00142 }
00143 void OmgListWidget::showOptionsTool()
00144 {
00145   toolConfigure->show();
00146 }
00147 void OmgListWidget::hideAddItemTool()
00148 {
00149   toolAddItem->hide();
00150 }
00151 void OmgListWidget::showAddItemTool()
00152 {
00153   toolAddItem->show();
00154 }
00156 // All other slots should use exactly the api format of QListWidget !!
00158 void OmgListWidget::addItem ( const QString & theLabel , bool theCheckedFlag=false)
00159 {
00160     QListWidgetItem *mypItem = new QListWidgetItem(theLabel,lstItems);
00161     //delegate
00162     addItem (mypItem,theCheckedFlag);
00163 }
00164 void OmgListWidget::addItem ( QListWidgetItem * thepItem, bool theCheckedFlag=false )
00165 {
00166   //for our custom widget, always display a checkbox
00167     if (!theCheckedFlag)
00168     {
00169       thepItem->setCheckState(Qt::Unchecked);
00170     }
00171     else
00172     {
00173       thepItem->setCheckState(Qt::Checked);
00174     }
00175     lstItems->addItem(thepItem);
00176 }
00177 void OmgListWidget::addItems ( const QStringList & theLabels , bool theCheckedFlag=false)
00178 {
00179   //using qts java style iterator...
00180   QStringListIterator myIterator(theLabels);
00181   while (myIterator.hasNext())
00182   {
00183     //get next iterator and delegate
00184     addItem(myIterator.next(),theCheckedFlag);
00185   }
00186 }
00187 void OmgListWidget::clear()
00188 {
00189         lstItems->clear();
00190 }
00191 int OmgListWidget::count()
00192 {
00193         return lstItems->count();
00194 }
00195 
00196 QListWidgetItem * OmgListWidget::item(int theItem)
00197 {
00198         return lstItems->item(theItem);
00199 }
00200 void OmgListWidget::insertItem ( int theRow, QListWidgetItem * thepItem , bool theCheckedFlag=false)
00201 {
00202     if (!theCheckedFlag)
00203     {
00204       thepItem->setCheckState(Qt::Unchecked);
00205     }
00206     else
00207     {
00208       thepItem->setCheckState(Qt::Checked);
00209     }
00210     lstItems->insertItem(theRow,thepItem);
00211 }
00212 void OmgListWidget::insertItem ( int theRow, const QString & theLabel , bool theCheckedFlag=false)
00213 {
00214     QListWidgetItem *mypItem = new QListWidgetItem(theLabel,lstItems);
00215     //delegate
00216     insertItem (theRow,mypItem,theCheckedFlag);
00217 }
00218 void OmgListWidget::insertItems ( int theRow, const QStringList & theLabels , bool theCheckedFlag=false)
00219 {
00220   //using qts java style iterator...
00221   QStringListIterator myIterator(theLabels);
00222   while (myIterator.hasNext())
00223   {
00224     //get next iterator and delegate
00225     insertItem(theRow++,myIterator.next(),theCheckedFlag);
00226   }
00227   
00228 }
00229 
00230 
00231 //
00232 // Private slots
00233 //
00234 
00235 void OmgListWidget::on_toolSelectAll_clicked()
00236 {
00237   //delegate
00238   selectAll();
00239 }
00240 void OmgListWidget::on_toolSelectNone_clicked()
00241 {
00242   //delegate
00243   selectNone();
00244 }
00245 
00246 void OmgListWidget::on_toolInvertSelection_clicked()
00247 {
00248   //delegate
00249   invertSelection();
00250 }
00251 
00252 void OmgListWidget::on_toolAddItem_clicked()
00253 {
00254   qDebug("on_toolAddItem_clicked()");
00255   emit addItemClicked();
00256 }
00257 
00258 void OmgListWidget::on_toolConfigure_clicked()
00259 {
00260   emit configureClicked(); 
00261 }

Generated on Mon Apr 28 15:09:56 2008 for openModellerDesktop by  doxygen 1.4.1-20050210