akregator/src

simplenodeselector.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "feedlist.h"
26 #include "folder.h"
27 #include "simplenodeselector.h"
28 #include "treenode.h"
29 #include "treenodevisitor.h"
30 
31 #include <tdelistview.h>
32 #include <tdelocale.h>
33 
34 #include <tqlayout.h>
35 #include <tqmap.h>
36 #include <tqwidget.h>
37 
38 namespace Akregator
39 {
40 
41 class SelectNodeDialog::SelectNodeDialogPrivate
42 {
43  public:
44  SimpleNodeSelector* widget;
45 };
46 
47 SelectNodeDialog::SelectNodeDialog(FeedList* feedList, TQWidget* parent, char* name) :
48  KDialogBase(parent, name, true, i18n("Select Feed or Folder"),
49  KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true), d(new SelectNodeDialogPrivate)
50 {
51  d->widget = new SimpleNodeSelector(feedList, this);
52 
53  connect(d->widget, TQ_SIGNAL(signalNodeSelected(TreeNode*)), this, TQ_SLOT(slotNodeSelected(TreeNode*)));
54 
55  setMainWidget(d->widget);
56  enableButtonOK(false);
57 }
58 
59 SelectNodeDialog::~SelectNodeDialog()
60 {
61  delete d;
62  d = 0;
63 }
64 
65 TreeNode* SelectNodeDialog::selectedNode() const
66 {
67  return d->widget->selectedNode();
68 }
69 
70 void SelectNodeDialog::slotSelectNode(TreeNode* node)
71 {
72  d->widget->slotSelectNode(node);
73 }
74 
75 void SelectNodeDialog::slotNodeSelected(TreeNode* node)
76 {
77  enableButtonOK(node != 0);
78 }
79 
80 
81 class SimpleNodeSelector::SimpleNodeSelectorPrivate
82 {
83  public:
84  TDEListView* view;
85  FeedList* list;
86  NodeVisitor* visitor;
87  TQMap<TreeNode*,TQListViewItem*> nodeToItem;
88  TQMap<TQListViewItem*, TreeNode*> itemToNode;
89 };
90 
91 class SimpleNodeSelector::NodeVisitor : public TreeNodeVisitor
92 {
93  public:
94 
95  NodeVisitor(SimpleNodeSelector* view) : TreeNodeVisitor(), m_view(view) {}
96  virtual ~NodeVisitor() {}
97 
98  void createItems(TreeNode* node)
99  {
100  node->accept(this);
101  }
102 
103  virtual bool visitFolder(Folder* node)
104  {
105  visitTreeNode(node);
106  TQValueList<TreeNode*> children = node->children();
107  m_view->d->nodeToItem[node]->setExpandable(true);
108  for (TQValueList<TreeNode*>::ConstIterator it = children.begin(); it != children.end(); ++it)
109  createItems(*it);
110  return true;
111  }
112 
113  virtual bool visitTreeNode(TreeNode* node)
114  {
115  TQListViewItem* pi = node->parent() ? m_view->d->nodeToItem[node->parent()] : 0;
116 
117  TDEListViewItem* item = 0;
118  if (pi != 0)
119  item = new TDEListViewItem(pi, node->title());
120  else
121  item = new TDEListViewItem(m_view->d->view, node->title());
122  item->setExpandable(false);
123  m_view->d->nodeToItem.insert(node, item);
124  m_view->d->itemToNode.insert(item, node);
125  connect(node, TQ_SIGNAL(signalDestroyed(TreeNode*)), m_view, TQ_SLOT(slotNodeDestroyed(TreeNode*)));
126  return true;
127  }
128 
129  private:
130 
131  SimpleNodeSelector* m_view;
132 };
133 
134 
135 SimpleNodeSelector::SimpleNodeSelector(FeedList* feedList, TQWidget* parent, const char* name) : TQWidget(parent, name), d(new SimpleNodeSelectorPrivate)
136 {
137  d->list = feedList;
138  connect(feedList, TQ_SIGNAL(signalDestroyed(FeedList*)), this, TQ_SLOT(slotFeedListDestroyed(FeedList*)));
139 
140  d->view = new TDEListView(this);
141  d->view->setRootIsDecorated(true);
142  d->view->addColumn(i18n("Feeds"));
143 
144  connect(d->view, TQ_SIGNAL(selectionChanged(TQListViewItem*)), this, TQ_SLOT(slotItemSelected(TQListViewItem*)));
145 
146  TQGridLayout* layout = new TQGridLayout(this, 1, 1);
147  layout->addWidget(d->view, 0, 0);
148 
149  d->visitor = new NodeVisitor(this);
150 
151  d->visitor->createItems(d->list->rootNode());
152  d->nodeToItem[d->list->rootNode()]->setOpen(true);
153  d->view->ensureItemVisible(d->nodeToItem[d->list->rootNode()]);
154 }
155 
156 SimpleNodeSelector::~SimpleNodeSelector()
157 {
158  delete d->visitor;
159  delete d;
160  d = 0;
161 }
162 
163 TreeNode* SimpleNodeSelector::selectedNode() const
164 {
165  return d->itemToNode[d->view->selectedItem()];
166 }
167 
168 void SimpleNodeSelector::slotSelectNode(TreeNode* node)
169 {
170  TQListViewItem* item = d->nodeToItem[node];
171  if (item != 0)
172  d->view->setSelected(item, true);
173 }
174 
175 void SimpleNodeSelector::slotFeedListDestroyed(FeedList* /*list*/)
176 {
177  d->nodeToItem.clear();
178  d->itemToNode.clear();
179  d->view->clear();
180 }
181 
182 void SimpleNodeSelector::slotItemSelected(TQListViewItem* item)
183 {
184  emit signalNodeSelected(d->itemToNode[item]);
185 }
186 
187 void SimpleNodeSelector::slotNodeDestroyed(TreeNode* node)
188 {
189  if (d->nodeToItem.contains(node))
190  {
191  TQListViewItem* item = d->nodeToItem[node];
192  d->nodeToItem.remove(node);
193  d->itemToNode.remove(item);
194  delete item;
195  }
196 }
197 
198 } // namespace Akregator
199 
200 #include "simplenodeselector.moc"
201