[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfflowlistwidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfflowlistwidget.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #include <QWidget>
00025 #include <QLabel>
00026 
00027 #include <klfdefs.h>
00028 
00029 #include "klfflowlayout.h"
00030 #include "klfflowlistwidget.h"
00031 #include "klfflowlistwidget_p.h"
00032 
00033 
00034 KLFFlowListWidget::KLFFlowListWidget(QWidget *parent) : QWidget(parent)
00035 {
00036   pLayout = new KLFFlowLayout(this);
00037   pWidgets = QList<QWidget*>();
00038 
00039   pLayout->setFlush(KLFFlowLayout::FlushBegin);
00040 
00041   setMinimumSize(1,1);
00042 }
00043 KLFFlowListWidget::~KLFFlowListWidget()
00044 {
00045 }
00046 
00047 
00048 QString KLFFlowListWidget::itemAt(int i) const
00049 {
00050   KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
00051                        "index "<<i<<" out of bounds [0,"<<pWidgets.size()<<"] !",
00052                        return QString(); ) ;
00053   return pWidgets[i]->property("klfflowlistwidget_str").toString();
00054 }
00055 QVariant KLFFlowListWidget::itemDataAt(int i) const
00056 {
00057   KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
00058                        "index "<<i<<" out of bounds [0,"<<pWidgets.size()<<"] !",
00059                        return QString(); ) ;
00060   return pWidgets[i]->property("klfflowlistwidget_data");
00061 }
00062 
00063 QStringList KLFFlowListWidget::itemList() const
00064 {
00065   QStringList s;
00066   int k;
00067   for (k = 0; k < pWidgets.size(); ++k) {
00068     s << itemAt(k);
00069   }
00070   return s;
00071 }
00072 QVariantList KLFFlowListWidget::itemDataList() const
00073 {
00074   QVariantList v;
00075   int k;
00076   for (k = 0; k < pWidgets.size(); ++k) {
00077     v << itemDataAt(k);
00078   }
00079   return v;
00080 }
00081 
00082 void KLFFlowListWidget::setItems(const QStringList& strings, const QVariantList& datalist)
00083 {
00084   // clean pWidgets
00085   while (pWidgets.size()) {
00086     delete pWidgets.takeAt(0);
00087   }
00088 
00089   KLF_ASSERT_CONDITION(strings.size() >= datalist.size(),
00090                        "datalist is larger than strings; some datas will be ignored.", ; ) ;
00091   // and individually add all items
00092   int k;
00093   for (k = 0; k < strings.size(); ++k) {
00094     QVariant v = (k < datalist.size()) ? datalist[k] : QVariant();
00095     addItem(strings[k], v);
00096   }
00097 }
00098 void KLFFlowListWidget::addItem(const QString& string, const QVariant& data)
00099 {
00100   insertItem(-1, string, data);
00101 }
00102 
00103 void KLFFlowListWidget::removeItem(int i)
00104 {
00105   i = simple_wrapped_item_index(i);
00106   KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
00107                        "index "<<i<<" out of bounds [0,"<<pWidgets.size()-1<<"] !",
00108                        return; ) ;
00109 
00110   delete pWidgets[i];
00111   pWidgets.removeAt(i);
00112 }
00113 
00114 void KLFFlowListWidget::insertItem(int i, const QString& s, const QVariant& data)
00115 {
00116   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00117   klfDbg("at pos="<<i<<", s="<<s<<", data="<<data) ;
00118 
00119   i = simple_wrapped_item_index(i);
00120   KLF_ASSERT_CONDITION(i >= 0 && i <= pWidgets.size(),
00121                        "index "<<i<<" out of bounds [0,"<<(pWidgets.size()-1)+1<<"] !",
00122                        return; ) ;
00123 
00124   KLFFlowListItemWidget *w = new KLFFlowListItemWidget(this);
00125   QLabel *l = new QLabel(s, this);
00126   w->setItemWidget(l);
00127 
00128   w->setProperty("klfflowlistwidget_str", QVariant(s));
00129   w->setProperty("klfflowlistwidget_data", data);
00130 
00131   connect(w, SIGNAL(closeClicked()), this, SLOT(itemClosed()));
00132 
00133   pLayout->addWidget(w, 0, 0, Qt::AlignVCenter);
00134 
00135   pWidgets.insert(i, w);
00136 }
00137 
00138 void KLFFlowListWidget::itemClosed()
00139 {
00140   KLFFlowListItemWidget *w = qobject_cast<KLFFlowListItemWidget*>(sender());
00141   KLF_ASSERT_NOT_NULL(w, "sender is not a KLFFlowListItemWidget !", return; ) ;
00142 
00143   klfDbg("removing item "<<w<<" ...") ;
00144 
00145   pWidgets.removeAll(w);
00146   w->deleteLater();
00147 }

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net