add check dialog
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Wed, 5 Mar 2008 13:25:01 +0000 (13:25 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Wed, 5 Mar 2008 13:25:01 +0000 (13:25 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@104 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/checkdlg.cpp [new file with mode: 0644]
src/checkdlg.h [new file with mode: 0644]

diff --git a/src/checkdlg.cpp b/src/checkdlg.cpp
new file mode 100644 (file)
index 0000000..6362073
--- /dev/null
@@ -0,0 +1,104 @@
+//
+// C++ Implementation: checkdlg
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "checkdlg.h"
+
+#include <QCheckBox>
+#include <QScrollArea>
+#include <QPushButton>
+#include <QBoxLayout>
+
+MCheckDialog::MCheckDialog(QWidget*parent,const MCheckList&checks,QString title)
+       :QDialog(parent),m_list(checks)
+{
+       setWindowTitle(title);
+       //basic layout
+       QVBoxLayout *vl=new QVBoxLayout;
+       setLayout(vl);
+       QScrollArea *sa=new QScrollArea;
+       vl->addWidget(sa,10);
+       QHBoxLayout *hl=new QHBoxLayout;
+       vl->addLayout(hl,0);
+       hl->addStretch(10);
+       QPushButton*p;
+       hl->addWidget(p=new QPushButton(tr("Ok")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(accept()));
+       hl->addWidget(p=new QPushButton(tr("Cancel")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+       //fill area
+       QWidget*w=new QWidget;
+       w->setLayout(vl=new QVBoxLayout);
+       for(int i=0;i<m_list.size();i++){
+               QCheckBox *cb=new QCheckBox(m_list[i].label());
+               cb->setChecked(m_list[i].isSet());
+               vl->addWidget(cb);
+               m_boxes.append(cb);
+       }
+       sa->setWidget(w);
+}
+
+MCheckList MCheckDialog::getCheckList()const
+{
+       for(int i=0;i<m_boxes.size();i++)
+               m_list[i].set(m_boxes[i]->isChecked());
+       return m_list;
+}
+
+/**************************************************/
+
+MCheckItem::MCheckItem(){}
+MCheckItem::~MCheckItem(){}
+
+QString MCheckItem::key()const{return label();}
+
+MCheckList::MCheckList(){}
+
+MCheckList::MCheckList(const MCheckList&l)
+{
+       for(int i=0;i<m_items.size();i++)
+               delete m_items[i];
+       m_items.clear();
+       for(int i=0;i<l.m_items.size();i++)
+               m_items.append(l.m_items[i]->copy());
+}
+
+MCheckList& MCheckList::operator=(const MCheckList&l)
+{
+       for(int i=0;i<l.m_items.size();i++)
+               m_items.append(l.m_items[i]->copy());
+}
+
+MCheckList::~MCheckList()
+{
+       for(int i=0;i<m_items.size();i++)
+               delete m_items[i];
+}
+
+void MCheckList::addItem(MCheckItem*i)
+{
+       if(i!=0)m_items.append(i);
+}
+
+int MCheckList::size()const
+{
+       return m_items.size();
+}
+
+MCheckItem& MCheckList::operator[](int i)
+{
+       return * m_items[i];
+}
+
+const MCheckItem& MCheckList::operator[](int i)const
+{
+       return * m_items[i];
+}
diff --git a/src/checkdlg.h b/src/checkdlg.h
new file mode 100644 (file)
index 0000000..e96cddb
--- /dev/null
@@ -0,0 +1,87 @@
+//
+// C++ Interface: checkdlg
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_CHECKDLG_H
+#define MAGICSMOKE_CHECKDLG_H
+
+#include <QDialog>
+#include <QList>
+
+class QCheckBox;
+
+/**abstract base class for items that can be displayed in a MCheckDialog*/
+class MCheckItem
+{
+       public:
+               /**constructs an empty check item*/
+               MCheckItem();
+               /**deletes the check item*/
+               virtual ~MCheckItem();
+               
+               /**overwrite this to return a label that can be displayed*/
+               virtual QString label()const=0;
+               /**overwrite this to return a key string that identifies the item (default implementation returns the label)*/
+               virtual QString key()const;
+               /**overwrite this to return whether the item is checked*/
+               virtual bool isSet()const=0;
+               /**overwrite this to change the checking status of the item*/
+               virtual void set(bool)=0;
+               
+       protected:
+               friend class MCheckList;
+               /**overwrite this to creat an exact copy of the item (must be implemented and must return non-NULL)*/
+               virtual MCheckItem* copy()const=0;
+};
+
+/**implements a list of checkable items*/
+class MCheckList
+{
+       public:
+               /**instantiate an empty list*/
+               MCheckList();
+               /**creates an exact copy of the list (uses MCheckItem::copy() )*/
+               MCheckList(const MCheckList&);
+               /**deletes the list*/
+               ~MCheckList();
+               
+               /**adds an item to the list*/
+               void addItem(MCheckItem*);
+               /**returns the size of the list*/
+               int size()const;
+               
+               /**returns a writable reference to an item*/
+               MCheckItem& operator[](int);
+               /**returns a const reference to an item*/
+               const MCheckItem& operator[](int)const;
+               
+               /**makes this list an exact copy of the argument*/
+               MCheckList& operator=(const MCheckList&);
+       private:
+               QList<MCheckItem*> m_items;
+};
+
+/**a dialog that consists of check boxes in a QScrollArea */
+class MCheckDialog:public QDialog
+{
+       Q_OBJECT
+       public:
+               /**creates the dialog*/
+               MCheckDialog(QWidget*parent,const MCheckList&checks,QString title);
+               
+               /**returns the current state of all check boxes, the return value is an exact copy of the check list from the constructor with updated check settings*/
+               MCheckList getCheckList()const;
+       private:
+               QList<QCheckBox*>m_boxes;
+               mutable MCheckList m_list;
+};
+
+#endif