summaryrefslogtreecommitdiffstatshomepage
path: root/CompositionButton.cpp
blob: 32f43b1c5024519c5de74b7719618ea0a3d1fb73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "CompositionButton.h"
#include <QGridLayout>
#include <QLabel>
#include <QEvent>
#include <QMouseEvent>
#include <QLineEdit>

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);
		}
	}
}