diff options
Diffstat (limited to 'Swift/QtUI/QtStatusWidget.cpp')
-rw-r--r-- | Swift/QtUI/QtStatusWidget.cpp | 161 |
1 files changed, 138 insertions, 23 deletions
diff --git a/Swift/QtUI/QtStatusWidget.cpp b/Swift/QtUI/QtStatusWidget.cpp index 3de2641..c6b0464 100644 --- a/Swift/QtUI/QtStatusWidget.cpp +++ b/Swift/QtUI/QtStatusWidget.cpp @@ -9,40 +9,155 @@ #include <QBoxLayout> #include <QComboBox> #include <QLineEdit> - +#include <QLabel> +#include <QFrame> +#include <QPoint> +#include <QStackedWidget> +#include <QApplication> +#include <QDesktopWidget> +#include <qdebug.h> +#include <QListWidget> +#include <QListWidgetItem> namespace Swift { -QtStatusWidget::QtStatusWidget(QWidget *parent) : QWidget(parent) { - types_ = new QComboBox(this); - types_->addItem(QIcon(":/icons/online.png"), "Available", QVariant(StatusShow::Online)); - types_->addItem(QIcon(":/icons/online.png"), "Free For Chat", QVariant(StatusShow::FFC)); - types_->addItem(QIcon(":/icons/away.png"), "Away", QVariant(StatusShow::Away)); - types_->addItem(QIcon(":/icons/away.png"), "Extended Away", QVariant(StatusShow::XA)); - types_->addItem(QIcon(":/icons/dnd.png"), "Do Not Disturb", QVariant(StatusShow::DND)); - types_->addItem(QIcon(":/icons/offline.png"), "Offline", QVariant(StatusShow::None)); - connect(types_, SIGNAL(activated(int)), this, SLOT(handleTypeSelected(int))); - QBoxLayout *mainLayout = new QBoxLayout(QBoxLayout::TopToBottom, this); - mainLayout->setContentsMargins(0,0,0,0); + +QtStatusWidget::QtStatusWidget(QWidget *parent) : QWidget(parent), editCursor_(Qt::IBeamCursor), viewCursor_(Qt::PointingHandCursor) { + isClicking_ = false; + setMaximumHeight(24); + + QHBoxLayout* mainLayout = new QHBoxLayout(this); mainLayout->setSpacing(0); - mainLayout->addWidget(types_); + mainLayout->setContentsMargins(0,0,0,0); + + stack_ = new QStackedWidget(this); + stack_->setLineWidth(2); + stack_->setFrameShape(QFrame::StyledPanel); + mainLayout->addWidget(stack_); + + QWidget* page1 = new QWidget(this); + stack_->addWidget(page1); + QHBoxLayout* page1Layout = new QHBoxLayout(page1); + page1Layout->setSpacing(0); + page1Layout->setContentsMargins(0,0,0,0); + page1->setCursor(viewCursor_); + + statusIcon_ = new QLabel(this); + statusIcon_->setMinimumSize(16, 16); + statusIcon_->setMaximumSize(16, 16); + page1Layout->addWidget(statusIcon_); + + statusTextLabel_ = new QLabel(this); + page1Layout->addWidget(statusTextLabel_); + + icons_[StatusShow::Online] = QIcon(":/icons/online.png"); + icons_[StatusShow::Away] = QIcon(":/icons/away.png"); + icons_[StatusShow::DND] = QIcon(":/icons/dnd.png"); + icons_[StatusShow::None] = QIcon(":/icons/offline.png"); + + setStatusType(StatusShow::None); + + QWidget* page2 = new QWidget(this); + QHBoxLayout* page2Layout = new QHBoxLayout(page2); + page2Layout->setSpacing(0); + page2Layout->setContentsMargins(0,0,0,0); + stack_->addWidget(page2); + + statusEdit_ = new QLineEdit(this); + page2Layout->addWidget(statusEdit_); + connect(statusEdit_, SIGNAL(returnPressed()), this, SLOT(handleEditComplete())); + connect(statusEdit_, SIGNAL(textChanged(const QString&)), this, SLOT(generateList())); + + setStatusText(""); + + + menu_ = new QListWidget(); + menu_->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); + menu_->setAlternatingRowColors(true); + + connect(menu_, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(handleItemClicked(QListWidgetItem*))); + viewMode(); } -void QtStatusWidget::handleTypeSelected(int index) { - Q_UNUSED(index); - emit onChangeStatusRequest(getSelectedStatusShow()); +QtStatusWidget::~QtStatusWidget() { + delete menu_; +} + +void QtStatusWidget::mousePressEvent(QMouseEvent*) { + handleClicked(); +} + +void QtStatusWidget::generateList() { + QString text = statusEdit_->text(); + menu_->clear(); + foreach (StatusShow::Type type, icons_.keys()) { + QListWidgetItem* item = new QListWidgetItem(text, menu_); + item->setIcon(icons_[type]); + item->setData(Qt::UserRole, QVariant(type)); + } +} + + +void QtStatusWidget::handleClicked() { + QPoint point = mapToGlobal(QPoint(0, height())); + int x = point.x(); + int y = point.y(); + int width = 200; + int height = 80; + int screenWidth = QApplication::desktop()->screenGeometry().width(); + if (x + width > screenWidth) { + x = screenWidth - width; + } + menu_->setGeometry(x, y, width, height); + menu_->setMaximumWidth(width); + QSizePolicy policy(menu_->sizePolicy()); + policy.setVerticalPolicy(QSizePolicy::Expanding); + menu_->setSizePolicy(policy); + menu_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + generateList(); + + menu_->show(); + activateWindow(); + stack_->setCurrentIndex(1); + statusEdit_->setFocus(); +} + +void QtStatusWidget::viewMode() { + menu_->hide(); + stack_->setCurrentIndex(0); +} + +void QtStatusWidget::handleEditComplete() { + statusText_ = statusEdit_->text(); + viewMode(); + emit onChangeStatusRequest(selectedStatusType_, statusText_); +} + +void QtStatusWidget::handleEditCancelled() { + setStatusText(statusText_); + viewMode(); } StatusShow::Type QtStatusWidget::getSelectedStatusShow() { - return (StatusShow::Type)types_->itemData(types_->currentIndex()).toInt(); + return selectedStatusType_; +} + +void QtStatusWidget::handleItemClicked(QListWidgetItem* item) { + selectedStatusType_ = (StatusShow::Type)(item->data(Qt::UserRole).toInt()); + handleEditComplete(); +} + + +void QtStatusWidget::setStatusText(const QString& text) { + statusText_ = text; + statusEdit_->setText(text); + QString escapedText(text.isEmpty() ? "(No message)" : text); + escapedText.replace("<","<"); + statusTextLabel_->setText("<i>" + escapedText + "</i>"); } void QtStatusWidget::setStatusType(StatusShow::Type type) { - for (int i = 0; i < types_->count(); i++) { - if (types_->itemData(i).toInt() == type) { - types_->setCurrentIndex(i); - break; - } - } + selectedStatusType_ = icons_.contains(type) ? type : StatusShow::Online; + statusIcon_->setPixmap(icons_[selectedStatusType_].pixmap(16, 16)); } } |