/* * Copyright (c) 2010 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include "Swift/QtUI/EventViewer/QtEventWindow.h" #include #include #include #include "Swiften/Events/MessageEvent.h" #include "Swiften/Events/ErrorEvent.h" #include "Swift/QtUI/QtSubscriptionRequestWindow.h" #include "Swiften/Events/SubscriptionRequestEvent.h" #include "Swift/Controllers/UIEvents/RequestChatUIEvent.h" #include "Swiften/Base/Platform.h" namespace Swift { QtEventWindow::QtEventWindow(UIEventStream* eventStream, QWidget* parent) : QWidget(parent) { QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, this); layout->setContentsMargins(0,0,0,0); layout->setSpacing(0); view_ = new QTreeView(this); layout->addWidget(view_); eventStream_ = eventStream; model_ = new EventModel(); view_->setModel(model_); delegate_ = new EventDelegate(); view_->setItemDelegate(delegate_); view_->setHeaderHidden(true); #ifdef SWIFT_PLATFORM_MACOSX view_->setAlternatingRowColors(true); #endif view_->setAnimated(true); view_->setIndentation(0); view_->setRootIsDecorated(true); readButton_ = new QPushButton("Display Notice", this); layout->addWidget(readButton_); readButton_->setEnabled(false); connect(readButton_, SIGNAL(clicked()), this, SLOT(handleReadClicked())); connect(view_, SIGNAL(clicked(const QModelIndex&)), this, SLOT(handleItemClicked(const QModelIndex&))); connect(view_, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&))); } QtEventWindow::~QtEventWindow() { delete model_; delete delegate_; /* Not view_ because this is the parent */ } void QtEventWindow::handleItemClicked(const QModelIndex&) { readButton_->setEnabled(true); } void QtEventWindow::handleReadClicked() { QModelIndex index = view_->currentIndex(); if (!index.isValid()) { return; } handleItemActivated(index); } void QtEventWindow::handleItemActivated(const QModelIndex& item) { QtEvent* event = model_->getItem(item.row()); boost::shared_ptr messageEvent = boost::dynamic_pointer_cast(event->getEvent()); boost::shared_ptr subscriptionEvent = boost::dynamic_pointer_cast(event->getEvent()); boost::shared_ptr errorEvent = boost::dynamic_pointer_cast(event->getEvent()); if (messageEvent) { eventStream_->send(boost::shared_ptr(new RequestChatUIEvent(messageEvent->getStanza()->getFrom()))); } else if (subscriptionEvent) { QtSubscriptionRequestWindow* window = QtSubscriptionRequestWindow::getWindow(subscriptionEvent, this); window->show(); } else if (errorEvent) { errorEvent->conclude(); } else { qWarning() << "Trying to activate an unexpected event"; } } void QtEventWindow::addEvent(boost::shared_ptr event, bool active) { model_->addEvent(event, active); emit onNewEventCountUpdated(model_->getNewEventCount()); } void QtEventWindow::removeEvent(boost::shared_ptr event) { model_->removeEvent(event); emit onNewEventCountUpdated(model_->getNewEventCount()); } }