~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/scripting/uiinterface.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Clementine.
2
 
   Copyright 2010, David Sansome <me@davidsansome.com>
3
 
 
4
 
   Clementine is free software: you can redistribute it and/or modify
5
 
   it under the terms of the GNU General Public License as published by
6
 
   the Free Software Foundation, either version 3 of the License, or
7
 
   (at your option) any later version.
8
 
 
9
 
   Clementine is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
   GNU General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU General Public License
15
 
   along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16
 
*/
17
 
 
18
 
#include "uiinterface.h"
19
 
 
20
 
#include <QAction>
21
 
#include <QMenu>
22
 
#include <QtDebug>
23
 
 
24
 
UIInterface::UIInterface(QObject* parent)
25
 
  : QObject(parent)
26
 
{
27
 
}
28
 
 
29
 
void UIInterface::RegisterActionLocation(const QString& id, QMenu* menu, QAction* before) {
30
 
  if (locations_.contains(id)) {
31
 
    qDebug() << __PRETTY_FUNCTION__
32
 
             << "A location with ID" << id << "was already registered";
33
 
    return;
34
 
  }
35
 
 
36
 
  locations_[id] = Location(menu, before);
37
 
 
38
 
  connect(menu, SIGNAL(destroyed()), SLOT(MenuDestroyed()));
39
 
  if (before) {
40
 
    connect(before, SIGNAL(destroyed()), SLOT(MenuActionDestroyed()));
41
 
  }
42
 
 
43
 
  // Add any actions that were waiting
44
 
  foreach (const IdAndAction& id_action, pending_actions_) {
45
 
    if (id_action.first == id) {
46
 
      DoAddAction(id_action.first, id_action.second);
47
 
      pending_actions_.removeAll(id_action);
48
 
    }
49
 
  }
50
 
}
51
 
 
52
 
void UIInterface::AddAction(const QString& id, QAction* action) {
53
 
  if (locations_.contains(id)) {
54
 
    DoAddAction(id, action);
55
 
  } else {
56
 
    // Maybe that part of the UI hasn't been lazy created yet
57
 
    pending_actions_ << IdAndAction(id, action);
58
 
    connect(action, SIGNAL(destroyed()), SLOT(ActionDestroyed()));
59
 
  }
60
 
}
61
 
 
62
 
void UIInterface::DoAddAction(const QString& id, QAction* action) {
63
 
  const Location& location = locations_[id];
64
 
 
65
 
  if (location.menu_) {
66
 
    location.menu_->insertAction(location.before_, action);
67
 
  }
68
 
}
69
 
 
70
 
void UIInterface::MenuDestroyed() {
71
 
  QMenu* menu = qobject_cast<QMenu*>(sender());
72
 
  foreach (const QString& id, locations_.keys()) {
73
 
    if (locations_[id].menu_ == menu) {
74
 
      locations_.remove(id);
75
 
    }
76
 
  }
77
 
}
78
 
 
79
 
void UIInterface::MenuActionDestroyed() {
80
 
  QAction* action = qobject_cast<QAction*>(sender());
81
 
  foreach (const QString& id, locations_.keys()) {
82
 
    if (locations_[id].before_ == action) {
83
 
      locations_.remove(id);
84
 
    }
85
 
  }
86
 
}
87
 
 
88
 
void UIInterface::ActionDestroyed() {
89
 
  QAction* action = qobject_cast<QAction*>(sender());
90
 
  foreach (const IdAndAction& id_action, pending_actions_) {
91
 
    if (id_action.second == action) {
92
 
      pending_actions_.removeAll(id_action);
93
 
    }
94
 
  }
95
 
}