kdeprint Library API Documentation

cupslist.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library 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 library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include "cupslist.h"
00021 
00022 #include <qlistview.h>
00023 #include <qpushbutton.h>
00024 #include <klocale.h>
00025 #include <qlayout.h>
00026 #include <qheader.h>
00027 
00028 #include "qinputbox.h"
00029 
00030 CupsListBox::CupsListBox(QWidget *parent, const char *name)
00031     : QWidget(parent, name)
00032 {
00033     init(1);
00034 }
00035 
00036 CupsListBox::CupsListBox(int columns, QWidget *parent, const char *name)
00037     : QWidget(parent, name)
00038 {
00039     init(columns);
00040 }
00041 
00042 CupsListBox::~CupsListBox()
00043 {
00044 }
00045 
00046 void CupsListBox::init(int columns)
00047 {
00048     list_ = 0;
00049     add_ = remove_ = 0;
00050     addmsg_.append(i18n("Enter new value:"));
00051 
00052     createView(columns);
00053     createButtons();
00054     createLayout();
00055 }
00056 
00057 void CupsListBox::createView(int columns)
00058 {
00059     if (list_ == 0)
00060     {
00061         list_ = new QListView(this);
00062         list_->setFrameStyle(QFrame::Sunken|QFrame::WinPanel);
00063         list_->setLineWidth(1);
00064         list_->setAllColumnsShowFocus(true);
00065         columns_ = columns;
00066         for (int i=0;i<columns_;i++)
00067             list_->addColumn("column");
00068         if (columns_ <= 1)
00069         {
00070             list_->header()->hide();
00071             list_->setMaximumHeight(70);
00072         }
00073     }
00074 }
00075 
00076 void CupsListBox::createLayout()
00077 {
00078 /*  QGridLayout *main_ = new QGridLayout(this, 2, 2, 0, 10);
00079     main_->addMultiCellWidget(list_, 0, 1, 0, 0);
00080     main_->addWidget(add_, 0, 1);
00081     main_->addWidget(remove_, 1, 1);*/
00082     QHBoxLayout *main_ = new QHBoxLayout(this, 0, 10);
00083     QVBoxLayout *btn_ = new QVBoxLayout(0, 0, 10);
00084     main_->addWidget(list_);
00085     main_->addLayout(btn_);
00086     btn_->addWidget(add_);
00087     btn_->addWidget(remove_);
00088     btn_->addStretch(1);
00089 }
00090 
00091 void CupsListBox::createButtons()
00092 {
00093     if (add_ == 0 && remove_ == 0)
00094     {
00095         add_ = new QPushButton(i18n("Add..."), this);
00096         connect(add_, SIGNAL(clicked()), SLOT(addClicked()));
00097         remove_ = new QPushButton(i18n("Remove"), this);
00098         connect(remove_, SIGNAL(clicked()), SLOT(removeClicked()));
00099     }
00100 }
00101 
00102 QListViewItem* CupsListBox::findItemAtIndex(int i) const
00103 {
00104     if (!list_ || i < 0 || i >= count()) return 0;
00105 
00106     QListViewItem   *item(list_->firstChild());
00107     int j(0);
00108     while (item)
00109     {
00110         if (j == i) break;
00111         else
00112         {
00113             item = item->nextSibling();
00114             j++;
00115         }
00116     }
00117     return item;
00118 }
00119 
00120 void CupsListBox::setColumnText(int column, const QString& txt)
00121 {
00122     if (column >= 0 && column < columns_ && list_)
00123     {
00124         list_->setColumnText(column, txt);
00125         if (!list_->header()->isVisible())
00126             list_->header()->show();
00127     }
00128 }
00129 
00130 QString CupsListBox::text(int index, int column) const
00131 {
00132     QListViewItem   *item = findItemAtIndex(index);
00133     if (item)
00134     {
00135         return item->text(column);
00136     }
00137     return QString::null;
00138 }
00139 
00140 void CupsListBox::insertItem(const QString& str, int index)
00141 {
00142     if (index == -1) index = count()-1;
00143     QListViewItem   *after = findItemAtIndex(index);
00144     if (after)
00145         new QListViewItem(list_, after, str);
00146     else
00147         new QListViewItem(list_, str);
00148 }
00149 
00150 void CupsListBox::insertItem(const QStringList& strs, int index)
00151 {
00152     if (index == -1) index = count()-1;
00153     QListViewItem   *after = findItemAtIndex(index), *item;
00154     if (after)
00155         item = new QListViewItem(list_, after);
00156     else
00157         item = new QListViewItem(list_);
00158     for (uint i=0;i<strs.count();i++)
00159         item->setText(i, *(strs.at(i)));
00160 }
00161 
00162 void CupsListBox::addClicked()
00163 {
00164     bool    ok;
00165     QStringList strs = QInputBox::inputBox(this, i18n("Add"), addmsg_, columns_, &ok);
00166     if (ok)
00167     {
00168         insertItem(strs);
00169     }
00170 
00171 }
00172 
00173 void CupsListBox::removeClicked()
00174 {
00175     if (list_->currentItem() != 0)
00176         delete list_->currentItem();
00177 }
00178 
00179 void CupsListBox::setAddMessage(const QString& msg, int index)
00180 {
00181     if (index >= 0 && index < (int)addmsg_.count())
00182         *(addmsg_.at(index)) = msg;
00183     else
00184         addmsg_.append(msg);
00185 }
00186 
00187 int CupsListBox::count() const
00188 {
00189     return (int)(list_->childCount());
00190 }
00191 #include "cupslist.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 5 07:21:38 2004 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003