summaryrefslogtreecommitdiff
path: root/src/qt/tabs_manager.cc
blob: 5d641d1cd59390eb9a4755d3f5238a0195f821c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** \file tabs_manager_impl.cc */
//
// Copyright (C) 2010 Piotr Galiszewski
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

// Local includes
#include "tabs_manager.h"

#include "widgets/tab_widget.h"
#include "widgets/changes_preview_tab.h"
#include "widgets/package_info_tab.h"
#include "widgets/packages_tab.h"
#include "widgets/perform_changes_tab.h"
#include "widgets/resolver_tab.h"
#include "windows/main_window.h"

#include "aptitude.h"

// System includes
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>

#include <QtGui/QApplication>

namespace aptitude
{
  namespace gui
  {
    namespace qt
    {
      class tabs_manager::tabs_manager_impl : public QObject, public tabs_manager
      {
	Q_OBJECT
	Q_DISABLE_COPY(tabs_manager_impl)

	/** \brief Pointer to the globally unique changes_preview tab,
	 *  or NULL if none exists.
	 */
	changes_preview_tab *changes_preview;

	/** \brief Pointer to the globally unique perform_changes tab,
	 *  or NULL if none exists.
	 */
	perform_changes_tab *perform_changes;

	/** \brief Pointer to the globally unique resolver tab,
	 *  or NULL if none exists.
	 */
	resolver_tab *resolver;

	boost::unordered_map<tab_widget *, boost::unordered_set<packages_tab *> > active_packages_tabs;

	/** \brief List of all opened package_info tabs.
	 *   \todo shouldn't this be unordered_map?
	 */
	boost::unordered_set<package_info_tab *> package_info_tabs;

	/** \brief List of all registered tab_widgets. */
	boost::unordered_set<tab_widget *> tab_widgets;

	/** \brief Update tabs close button for given tab_widget. */
	void update_packages_tabs_closability(tab_widget *tabs);

	/** \brief Connect all required signals from tab_widget. */
	void connect_tab_widget_signals(tab_widget *tabs);

	/** \brief Activate given tab. */
	void activate_tab(tab *tab_to_activate);

	/** \brief If the given pointer variable is not NULL, displays the
	 *  corresponding tab.
	 *
	 *  Otherwise, instantiates a new instance of its type using the
	 *  default constructor and displays that instance with the given
	 *  title, storing the new instance in the given variable.
	 */
	template<typename TabType>
	void create_or_display(TabType *output, tab_widget *tabs, const char *name);

      private Q_SLOTS:
	/** \brief Slot invoked when the user asks to close a tab
	 *  managed by this object.
	 *
	 * This slot checks whether the given tab should be closed.  If it
	 * is, the tab is deleted; otherwise, it is hidden.
	 */
	void tab_deletion_requested(tab *tab_to_delete);

	/** \brief Slot invoked when a tab_widget is destroyed
	 *
	 * This slot removes all references to the destroyed tab_widget from manager data.
	 */
	void tab_widget_destroyed();

	/** \brief Slot invoked when a tab is destroyed
	 *
	 * This slot removes all references to the destroyed tab from manager data.
	 */
	void tab_destroyed();

      public:
	/** \brief Create a new tabs_manager_impl. */
	explicit tabs_manager_impl();

	virtual ~tabs_manager_impl();

	/** \brief Display the changes_preview_tab, creating it if it does not exist.
	 *
	 *  The changes_preview_tab is globally unique.
	 */
	void open_changes_preview_tab(QWidget *tab_context);

	/** \brief Create a new packages_tab. */
	void open_packages_tab(QWidget *tab_context);

	/** \brief Display the package_info_tab corresponding to the given
	 *  package.
	 *
	 *  If a package_info_tab is already displayed for this package, it
	 *  is reused; otherwise, a new tab is created and displayed.
	 */
	void open_package_info_tab(QWidget *tab_context);

	/** \brief Display the perform_changes_tab, creating it if it does not exist.
	 *
	 *  The perform_changes_tab is globally unique.
	 */
	void open_perform_changes_tab(QWidget *tab_context);

	/** \brief Display the resolver tab, creating it if it does not exist.
	 *
	 *  The resolver tab is globally unique.
	 */
	void open_resolver_tab(QWidget *tab_context);
      };

      tabs_manager::tabs_manager_impl::tabs_manager_impl()
	: changes_preview(0), perform_changes(0), resolver(0)
      {
      }

      tabs_manager::tabs_manager_impl::~tabs_manager_impl()
      {
      }

      void tabs_manager::tabs_manager_impl::update_packages_tabs_closability(tab_widget *tabs)
      {
	const boost::unordered_set<packages_tab *> &packages_tabs = active_packages_tabs[tabs];

	if(packages_tabs.size() == 1)
	  tabs->set_tab_closable((*packages_tabs.begin()), false);
	else if(packages_tabs.size() == 2)
	{
	  for(boost::unordered_set<packages_tab *>::const_iterator it =
	      packages_tabs.begin(); it != packages_tabs.end(); ++it)
	    tabs->set_tab_closable(*it, true);
	}
      }

      void tabs_manager::tabs_manager_impl::connect_tab_widget_signals(tab_widget *tabs)
      {
	if(tab_widgets.find(tabs) == tab_widgets.end())
	{
	  tab_widgets.insert(tabs);
	  connect(tabs, SIGNAL(tab_deletion_request(tab *)), this, SLOT(tab_deletion_requested(tab *)));
	  connect(tabs, SIGNAL(destroyed()), this, SLOT(tab_widget_destroyed()));
	}
      }

      void tabs_manager::tabs_manager_impl::activate_tab(tab *tab_to_activate)
      {
	main_window *window = qobject_cast<main_window *>(tab_to_activate->window());
	if(window == NULL)
	  return;

	if(!window->isActiveWindow())
	  qApp->alert(window);

	tab_to_activate->show();

	window->get_tab_widget()->setCurrentWidget(tab_to_activate);
      }

      template<typename TabType>
      void tabs_manager::tabs_manager_impl::create_or_display(TabType *output, tab_widget *tabs, const char *name)
      {
        if(output == NULL)
          {
            connect_tab_widget_signals(tabs);

            output = new TabType;
            connect(output, SIGNAL(destroyed()), this, SLOT(tab_destroyed()));

            tabs->addTab(output, name);
          }

        activate_tab(output);
      }

      void tabs_manager::tabs_manager_impl::open_changes_preview_tab(QWidget *tab_context)
      {
	main_window *window = qobject_cast<main_window *>(tab_context->window());
	if(window == NULL)
	  return;

	create_or_display<changes_preview_tab>(changes_preview, window->get_tab_widget(), _("Preview"));
      }

      void tabs_manager::tabs_manager_impl::open_package_info_tab(QWidget *tab_context)
      {
	main_window *window = qobject_cast<main_window *>(tab_context->window());
	if(window == NULL)
	  return;
      }

      void tabs_manager::tabs_manager_impl::open_packages_tab(QWidget *tab_context)
      {
	main_window *window = qobject_cast<main_window *>(tab_context->window());
	if(window == NULL)
	  return;

	tab_widget *widget = window->get_tab_widget();

	connect_tab_widget_signals(widget);

	packages_tab *tab = new packages_tab();
	widget->addTab(tab, _("Packages"));
	widget->setCurrentWidget(tab);

	active_packages_tabs[widget].insert(tab);
	update_packages_tabs_closability(widget);
      }

      void tabs_manager::tabs_manager_impl::open_perform_changes_tab(QWidget *tab_context)
      {
	main_window *window = qobject_cast<main_window *>(tab_context->window());
	if(window == NULL)
	  return;

	create_or_display<perform_changes_tab>(perform_changes, window->get_tab_widget(), _("Perform Changes"));
      }

      void tabs_manager::tabs_manager_impl::open_resolver_tab(QWidget *tab_context)
      {
	main_window *window = qobject_cast<main_window *>(tab_context->window());
	if(window == NULL)
	  return;

	create_or_display<resolver_tab>(resolver, window->get_tab_widget(), _("Resolver"));
      }

      void tabs_manager::tabs_manager_impl::tab_deletion_requested(tab *tab_to_delete)
      {
	// when for example performing changes tab is closed, we do not destroy it but only hide
	if(tab_to_delete->get_policy() == tab::closing_policy_hide)
	    tab_to_delete->hide();
	else if(tab_to_delete->get_type() == tab::tab_packages)
	{
	  main_window *window = qobject_cast<main_window *>(tab_to_delete->window());
	  if(window == NULL)
	    return;

	  active_packages_tabs[window->get_tab_widget()].erase(qobject_cast<packages_tab *>(tab_to_delete));

	  update_packages_tabs_closability(window->get_tab_widget());

	  tab_to_delete->deleteLater();
	}
	else if(tab_to_delete->get_type() == tab::tab_info)
	{
	  package_info_tabs.erase(qobject_cast<package_info_tab *>(tab_to_delete));

	  tab_to_delete->deleteLater();
	}
      }

      void tabs_manager::tabs_manager_impl::tab_widget_destroyed()
      {
	tab_widget *widget = qobject_cast<tab_widget *>(sender());
	if(widget != NULL)
	{
	  tab_widgets.erase(widget);
	  active_packages_tabs.erase(widget);
	}
      }

      void tabs_manager::tabs_manager_impl::tab_destroyed()
      {
	if(qobject_cast<perform_changes_tab *>(sender()) != NULL)
	  perform_changes = NULL;
	else if(qobject_cast<resolver_tab *>(sender()) != NULL)
	  resolver = NULL;
	else if(qobject_cast<changes_preview_tab *>(sender()) != NULL)
	  changes_preview = NULL;
      }

      tabs_manager *tabs_manager::get_instance()
      {
	static tabs_manager_impl instance;

	return &instance;
      }

      tabs_manager::tabs_manager()
      {
      }

      tabs_manager::~tabs_manager()
      {
      }
    }
  }
}

#include "tabs_manager.mocc"