#include "CompositionButton.h" #include #include #include #include #include CompositionButton::CompositionButton(const QString &title, const QDateTime &date, QWidget *parent) : QWidget(parent) { setAttribute(Qt::WA_Hover, true); setCursor(QCursor(Qt::PointingHandCursor)); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); m_layout = new QGridLayout; m_layout->setContentsMargins(6, 6, 6, 6); m_layout->setVerticalSpacing(0); m_layout->setHorizontalSpacing(7); m_arrow = new QLabel(QLatin1String(">")); m_arrow->setStyleSheet(QLatin1String("color: gray; font-family: \"CMU Typewriter Text Variable Width\"")); m_layout->addWidget(m_arrow, 0, 0, 2, 1, Qt::AlignVCenter); m_title = new QLabel(title); m_title->setStyleSheet(QLatin1String("color: black; font-weight: bold; font-family: \"CMU Typewriter Text Variable Width\"")); m_edit = new QLineEdit; m_edit->setStyleSheet(m_title->styleSheet()); m_edit->setMaximumHeight(m_title->height()); m_layout->addWidget(m_title, 0, 1); m_date = new QLabel(date.toString()); m_date->setStyleSheet(QLatin1String("color: gray; font-family: \"CMU Typewriter Text Variable Width\"")); m_layout->addWidget(m_date, 1, 1); setLayout(m_layout); } CompositionButton::~CompositionButton() { m_title->deleteLater(); m_edit->deleteLater(); } QString CompositionButton::title() const { return m_title->text(); } QString CompositionButton::editedTitle() const { return m_edit->text(); } bool CompositionButton::event(QEvent *event) { if (m_arrow) { if (event->type() == QEvent::HoverEnter) m_arrow->setStyleSheet(QLatin1String("color: black; font-family: \"CMU Typewriter Text Variable Width\"")); else if (event->type() == QEvent::HoverLeave) m_arrow->setStyleSheet(QLatin1String("color: gray; font-family: \"CMU Typewriter Text Variable Width\"")); } return QWidget::event(event); } void CompositionButton::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) emit selected(); QWidget::mousePressEvent(event); } bool CompositionButton::editMode() const { return m_arrow->text() == QLatin1String("X"); } bool CompositionButton::deleted() const { return !m_date->isVisible(); } void CompositionButton::setDeleted(bool deleted) { if (deleted && editMode()) { m_layout->removeWidget(m_edit); m_edit->setParent(0); m_layout->addWidget(m_title, 0, 1); } else if (!deleted && editMode()) { m_layout->removeWidget(m_title); m_title->setParent(0); m_layout->addWidget(m_edit, 0, 1); } m_title->setStyleSheet(deleted ? QLatin1String("text-decoration: line-through; color: gray; font-weight: bold; font-family: \"CMU Typewriter Text Variable Width\"") : QLatin1String("color: black; font-weight: bold; font-family: \"CMU Typewriter Text Variable Width\"")); m_date->setVisible(!deleted); } void CompositionButton::setEditMode(bool on, bool commit) { m_arrow->setText(on ? QLatin1String("X") : QLatin1String(">")); if (on) { m_edit->setText(m_title->text()); if (m_title->parent()) { m_layout->removeWidget(m_title); m_title->setParent(0); m_layout->addWidget(m_edit, 0, 1); } } else { setDeleted(false); if (commit) m_title->setText(m_edit->text()); if (m_edit->parent()) { m_layout->removeWidget(m_edit); m_edit->setParent(0); m_layout->addWidget(m_title, 0, 1); } } }