[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfflowlistwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfflowlistwidget.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfflowlistwidget.cpp 667 2011-07-24 15:44:22Z phfaist $ */
23 
24 #include <QWidget>
25 #include <QLabel>
26 
27 #include <klfdefs.h>
28 
29 #include "klfflowlayout.h"
30 #include "klfflowlistwidget.h"
31 #include "klfflowlistwidget_p.h"
32 
33 
34 KLFFlowListWidget::KLFFlowListWidget(QWidget *parent) : QWidget(parent)
35 {
36  pLayout = new KLFFlowLayout(this);
37  pWidgets = QList<QWidget*>();
38 
40 
41  setMinimumSize(1,1);
42 }
44 {
45 }
46 
47 
49 {
50  KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
51  "index "<<i<<" out of bounds [0,"<<pWidgets.size()<<"] !",
52  return QString(); ) ;
53  return pWidgets[i]->property("klfflowlistwidget_str").toString();
54 }
56 {
57  KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
58  "index "<<i<<" out of bounds [0,"<<pWidgets.size()<<"] !",
59  return QString(); ) ;
60  return pWidgets[i]->property("klfflowlistwidget_data");
61 }
62 
64 {
65  QStringList s;
66  int k;
67  for (k = 0; k < pWidgets.size(); ++k) {
68  s << itemAt(k);
69  }
70  return s;
71 }
72 QVariantList KLFFlowListWidget::itemDataList() const
73 {
74  QVariantList v;
75  int k;
76  for (k = 0; k < pWidgets.size(); ++k) {
77  v << itemDataAt(k);
78  }
79  return v;
80 }
81 
82 void KLFFlowListWidget::setItems(const QStringList& strings, const QVariantList& datalist)
83 {
84  // clean pWidgets
85  while (pWidgets.size()) {
86  delete pWidgets.takeAt(0);
87  }
88 
89  KLF_ASSERT_CONDITION(strings.size() >= datalist.size(),
90  "datalist is larger than strings; some datas will be ignored.", ; ) ;
91  // and individually add all items
92  int k;
93  for (k = 0; k < strings.size(); ++k) {
94  QVariant v = (k < datalist.size()) ? datalist[k] : QVariant();
95  addItem(strings[k], v);
96  }
97 }
98 void KLFFlowListWidget::addItem(const QString& string, const QVariant& data)
99 {
100  insertItem(-1, string, data);
101 }
102 
104 {
105  i = simple_wrapped_item_index(i);
106  KLF_ASSERT_CONDITION(i >= 0 && i < pWidgets.size(),
107  "index "<<i<<" out of bounds [0,"<<pWidgets.size()-1<<"] !",
108  return; ) ;
109 
110  delete pWidgets[i];
111  pWidgets.removeAt(i);
112 }
113 
114 void KLFFlowListWidget::insertItem(int i, const QString& s, const QVariant& data)
115 {
117  klfDbg("at pos="<<i<<", s="<<s<<", data="<<data) ;
118 
119  i = simple_wrapped_item_index(i);
120  KLF_ASSERT_CONDITION(i >= 0 && i <= pWidgets.size(),
121  "index "<<i<<" out of bounds [0,"<<(pWidgets.size()-1)+1<<"] !",
122  return; ) ;
123 
124  KLFFlowListItemWidget *w = new KLFFlowListItemWidget(this);
125  QLabel *l = new QLabel(s, this);
126  w->setItemWidget(l);
127 
128  w->setProperty("klfflowlistwidget_str", QVariant(s));
129  w->setProperty("klfflowlistwidget_data", data);
130 
131  connect(w, SIGNAL(closeClicked()), this, SLOT(itemClosed()));
132 
133  pLayout->addWidget(w, 0, 0, Qt::AlignVCenter);
134 
135  pWidgets.insert(i, w);
136 }
137 
138 void KLFFlowListWidget::itemClosed()
139 {
140  KLFFlowListItemWidget *w = qobject_cast<KLFFlowListItemWidget*>(sender());
141  KLF_ASSERT_NOT_NULL(w, "sender is not a KLFFlowListItemWidget !", return; ) ;
142 
143  klfDbg("removing item "<<w<<" ...") ;
144 
145  pWidgets.removeAll(w);
146  w->deleteLater();
147 }
A Layout that places widgets left to right, top to bottom.
Definition: klfflowlayout.h:38
virtual void addWidget(QWidget *w, int hstretch=0, int vstretch=0, Qt::Alignment align=0)
Base declarations for klatexformula and some utilities.
QVariantList itemDataList() const
void removeAt(int i)
void insertItem(int i, const QString &s, const QVariant &data=QVariant())
#define klfDbg(streamableItems)
print debug stream items
KLFFlowListWidget(QWidget *parent=NULL)
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
T takeAt(int i)
Leave all extra space at end of line.
Definition: klfflowlayout.h:52
int size() const
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
QString itemAt(int i) const
void setFlush(Flush f)
void addItem(const QString &string, const QVariant &data=QVariant())
int removeAll(const T &value)
QVariant itemDataAt(int i) const
#define KLF_FUNC_NAME
void setItems(const QStringList &strings, const QVariantList &datalist=QVariantList())
void insert(int i, const T &value)
#define KLF_ASSERT_CONDITION(expr, msg, failaction)
Asserting Conditions (NON-FATAL)
QStringList itemList() const

Generated by doxygen 1.8.13