1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2006-2009 Licq developers
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "treepager.h"
21 
22 #include <QHBoxLayout>
23 #include <QStackedLayout>
24 #include <QTreeWidget>
25 
26 using namespace LicqQtGui;
27 /* TRANSLATOR LicqQtGui::TreePager */
28 
TreePager(QWidget * parent)29 TreePager::TreePager(QWidget* parent)
30   : QWidget(parent)
31 {
32   QHBoxLayout* boxLayout = new QHBoxLayout(this);
33   boxLayout->setContentsMargins(0, 0, 0, 0);
34 
35   // Display page titles in a tree widget
36   myTreeList = new QTreeWidget;
37   myTreeList->setColumnCount(1);
38   myTreeList->setHeaderLabel(tr("Categories"));
39   // Minimize the widget to be able to dynamically enlarge it later.
40   myTreeList->resize(0, 0);
41   boxLayout->addWidget(myTreeList);
42 
43   // Hold the actual pages in a stack layout
44   myPageStack = new QStackedLayout;
45   boxLayout->addLayout(myPageStack, 1);
46 
47   // Connect signals
48   connect(myTreeList, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
49       SLOT(flipPage(QTreeWidgetItem*)));
50 }
51 
addPage(QWidget * page,const QString & title,QWidget * parent)52 void TreePager::addPage(QWidget* page, const QString& title, /* const QIcon& icon, */ QWidget* parent)
53 {
54   QTreeWidgetItem* parentItem = NULL;
55   if (parent != NULL)
56     parentItem = myPageMap.key(parent);
57 
58   QTreeWidgetItem* item;
59   if (parentItem == NULL)
60     item = new QTreeWidgetItem(myTreeList, QStringList(title));
61   else
62     item = new QTreeWidgetItem(parentItem, QStringList(title));
63 
64 //  item->setIcon(0, icon);
65   myTreeList->expandItem(item);
66   myTreeList->resizeColumnToContents(0);
67   myTreeList->setFixedWidth(myTreeList->columnWidth(0) + myTreeList->frameWidth() * 2);
68 
69   myPageStack->addWidget(page);
70   myPageMap.insert(item, page);
71 }
72 
showPage(QWidget * page)73 void TreePager::showPage(QWidget* page)
74 {
75   myPageStack->setCurrentWidget(page);
76   myTreeList->setCurrentItem(myPageMap.key(page));
77   emit currentPageChanged(page);
78 }
79 
currentPage() const80 QWidget* TreePager::currentPage() const
81 {
82   return myPageStack->currentWidget();
83 }
84 
flipPage(QTreeWidgetItem * selection)85 void TreePager::flipPage(QTreeWidgetItem* selection)
86 {
87   QWidget* w = myPageMap[selection];
88   if (w == NULL)
89     return;
90 
91   myPageStack->setCurrentWidget(w);
92   emit currentPageChanged(w);
93 }
94