root/trunk/qt4-gui/src/views/mmuserview.cpp

Revision 6352, 5.5 kB (checked in by flynd, 6 months ago)

Reverted r6351. If definitions from a class is directly used by a class, the header should be included.

  • Property svn:eol-style set to native
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of Licq, an instant messaging client for UNIX.
4 * Copyright (C) 2000-2006 Licq developers
5 *
6 * Licq 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 * Licq 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 Licq; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "mmuserview.h"
22
23#include "config.h"
24
25#include <ctype.h>
26
27#include <QDragEnterEvent>
28#include <QDropEvent>
29#include <QHeaderView>
30#include <QKeyEvent>
31#include <QMenu>
32#include <QMouseEvent>
33
34#include <licq_icqd.h>
35#include <licq_user.h>
36
37#include "config/contactlist.h"
38
39#include "contactlist/contactlist.h"
40#include "contactlist/multicontactproxy.h"
41
42#include "core/gui-defines.h"
43#include "core/messagebox.h"
44
45#include "helpers/usercodec.h"
46
47using namespace LicqQtGui;
48/* TRANSLATOR LicqQtGui::MMUserView */
49
50MMUserView::MMUserView(QString id, unsigned long ppid, ContactListModel* contactList, QWidget* parent)
51  : UserViewBase(contactList, parent),
52    myId(id),
53    myPpid(ppid)
54{
55  // Use a proxy model for sorting and filtering
56  myListProxy = new MultiContactProxy(myContactList, this);
57  setModel(myListProxy);
58  setRootIndex(dynamic_cast<MultiContactProxy*>(myListProxy)->rootIndex());
59
60  // Setup popup menu
61  myMenu = new QMenu();
62  myMenu->addAction(tr("Remove"), this, SLOT(remove()));
63  myMenu->addAction(tr("Crop"), this, SLOT(crop()));
64  myMenu->addAction(tr("Clear"), this, SLOT(clear()));
65  myMenu->addSeparator();
66  myMenu->addAction(tr("Add Group"), this, SLOT(addCurrentGroup()));
67  myMenu->addAction(tr("Add All"), this, SLOT(addAll()));
68
69  setSelectionMode(ExtendedSelection);
70  dynamic_cast<SortedContactListProxy*>(myListProxy)->sort(0);
71  header()->setVisible(Config::ContactList::instance()->showHeader());
72
73  for (unsigned short i = 0; i < Config::ContactList::instance()->columnCount(); i++)
74    setColumnWidth(i, Config::ContactList::instance()->columnWidth(i));
75}
76
77MMUserView::~MMUserView()
78{
79  // Empty
80}
81
82void MMUserView::add(QString id, unsigned long ppid)
83{
84  if (id == myId && ppid == myPpid)
85    return;
86  dynamic_cast<MultiContactProxy*>(myListProxy)->add(id, ppid);
87}
88
89void MMUserView::removeFirst()
90{
91  QPair<QString, unsigned long> contact = *contacts().begin();
92  dynamic_cast<MultiContactProxy*>(myListProxy)->remove(contact.first, contact.second);
93}
94
95const QSet<QPair<QString, unsigned long> >& MMUserView::contacts() const
96{
97  return dynamic_cast<MultiContactProxy*>(myListProxy)->contacts();
98}
99
100void MMUserView::remove()
101{
102  dynamic_cast<MultiContactProxy*>(myListProxy)->remove(selectedIndexes());
103}
104
105void MMUserView::crop()
106{
107  dynamic_cast<MultiContactProxy*>(myListProxy)->crop(selectedIndexes());
108}
109
110void MMUserView::clear()
111{
112  dynamic_cast<MultiContactProxy*>(myListProxy)->clear();
113}
114
115void MMUserView::addCurrentGroup()
116{
117  GroupType groupType = Config::ContactList::instance()->groupType();
118  unsigned long groupId = Config::ContactList::instance()->groupId();
119
120  dynamic_cast<MultiContactProxy*>(myListProxy)->addGroup(groupType, groupId);
121
122  // Make sure current user isn't added
123  dynamic_cast<MultiContactProxy*>(myListProxy)->remove(myId, myPpid);
124}
125
126void MMUserView::addAll()
127{
128  // Add all contacts from "All users" group
129  dynamic_cast<MultiContactProxy*>(myListProxy)->addGroup(GROUPS_SYSTEM, GROUP_ALL_USERS);
130
131  // Make sure current user isn't added
132  dynamic_cast<MultiContactProxy*>(myListProxy)->remove(myId, myPpid);
133}
134
135void MMUserView::dragEnterEvent(QDragEnterEvent* event)
136{
137  if (event->mimeData()->hasText())
138    event->acceptProposedAction();
139}
140
141void MMUserView::dropEvent(QDropEvent* event)
142{
143  // We ignore the event per default and then accept it if we
144  // get to the end of this function.
145  event->ignore();
146
147  if (event->mimeData()->hasText() && event->mimeData()->text().length() > 4)
148  {
149    QString text = event->mimeData()->text();
150
151    unsigned long ppid = 0;
152    FOR_EACH_PROTO_PLUGIN_START(gLicqDaemon)
153    {
154      if (text.startsWith(PPIDSTRING((*_ppit)->PPID())))
155      {
156        ppid = (*_ppit)->PPID();
157        break;
158      }
159    }
160    FOR_EACH_PROTO_PLUGIN_END;
161
162    if (ppid == 0)
163      return;
164
165    QString id = text.mid(4);
166
167    if (id.isEmpty())
168      return;
169
170    add(id, ppid);
171  }
172  else
173    return; // Not accepted
174
175  event->acceptProposedAction();
176}
177
178void MMUserView::mousePressEvent(QMouseEvent* event)
179{
180  UserViewBase::mousePressEvent(event);
181
182  if (event->button() == Qt::LeftButton)
183  {
184    if (!indexAt(event->pos()).isValid())
185    {
186      // Clicking outiside list will clear selection
187      selectionModel()->clearSelection();
188      setCurrentIndex(QModelIndex());
189    }
190  }
191}
192
193void MMUserView::contextMenuEvent(QContextMenuEvent* event)
194{
195  myMenu->popup(viewport()->mapToGlobal(event->pos()));
196}
197
198void MMUserView::keyPressEvent(QKeyEvent* event)
199{
200  if (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier))
201  {
202    event->ignore();
203    UserViewBase::keyPressEvent(event);
204    return;
205  }
206
207  switch (event->key())
208  {
209    case Qt::Key_Space:
210      myMenu->popup(viewport()->mapToGlobal(QPoint(40, visualRect(currentIndex()).y())));
211      return;
212
213    default:
214      UserViewBase::keyPressEvent(event);
215  }
216}
Note: See TracBrowser for help on using the browser.