summaryrefslogtreecommitdiffstats
blob: 9c0df5e7b15295d6e55ecad5c4685278bbd3634f (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (c) 2012 Maciej Niedzielski
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <QDataWidgetMapper>
#include <QStringListModel>
#include <QFileDialog>

#include <Swift/QtUI/QtHighlightRuleWidget.h>
#include <Swift/QtUI/QtHighlightRulesItemModel.h>

namespace Swift {

QtHighlightRuleWidget::QtHighlightRuleWidget(QWidget* parent)
	: QWidget(parent)
{
	ui_.setupUi(this);

	QStringList applyToItems;
	for (int i = 0; i < QtHighlightRulesItemModel::ApplyToEOL; ++i) {
		applyToItems << QtHighlightRulesItemModel::getApplyToString(i);
	}
	QStringListModel * applyToModel = new QStringListModel(applyToItems, this);
	ui_.applyTo->setModel(applyToModel);

	connect(ui_.highlightText, SIGNAL(toggled(bool)), SLOT(onHighlightTextToggled(bool)));
	connect(ui_.customColors, SIGNAL(toggled(bool)), SLOT(onCustomColorsToggled(bool)));
	connect(ui_.playSound, SIGNAL(toggled(bool)), SLOT(onPlaySoundToggled(bool)));
	connect(ui_.customSound, SIGNAL(toggled(bool)), SLOT(onCustomSoundToggled(bool)));
	connect(ui_.soundFileButton, SIGNAL(clicked()), SLOT(onSoundFileButtonClicked()));

	mapper_ = new QDataWidgetMapper(this);
	hasValidIndex_ = false;
	model_ = NULL;
}

QtHighlightRuleWidget::~QtHighlightRuleWidget()
{
}

/** Widget does not gain ownership over the model */
void QtHighlightRuleWidget::setModel(QtHighlightRulesItemModel* model)
{
	model_ = model;
	mapper_->setModel(model_);
}

void QtHighlightRuleWidget::setActiveIndex(const QModelIndex& index)
{
	if (index.isValid()) {
		if (!hasValidIndex_) {
			mapper_->addMapping(ui_.applyTo, QtHighlightRulesItemModel::ApplyTo, "currentIndex");
			mapper_->addMapping(ui_.senders, QtHighlightRulesItemModel::Sender, "plainText");
			mapper_->addMapping(ui_.keywords, QtHighlightRulesItemModel::Keyword, "plainText");
			mapper_->addMapping(ui_.nickIsKeyword, QtHighlightRulesItemModel::NickIsKeyword);
			mapper_->addMapping(ui_.matchCase, QtHighlightRulesItemModel::MatchCase);
			mapper_->addMapping(ui_.matchWholeWords, QtHighlightRulesItemModel::MatchWholeWords);
			mapper_->addMapping(ui_.highlightText, QtHighlightRulesItemModel::HighlightText);
			mapper_->addMapping(ui_.foreground, QtHighlightRulesItemModel::TextColor, "color");
			mapper_->addMapping(ui_.background, QtHighlightRulesItemModel::TextBackground, "color");
			mapper_->addMapping(ui_.playSound, QtHighlightRulesItemModel::PlaySound);
			mapper_->addMapping(ui_.soundFile, QtHighlightRulesItemModel::SoundFile);
		}
		mapper_->setCurrentModelIndex(index);
		ui_.customColors->setChecked(ui_.foreground->getColor().isValid() || ui_.background->getColor().isValid());
		ui_.customSound->setChecked(!ui_.soundFile->text().isEmpty());
		ui_.applyTo->focusWidget();
	} else {
		if (hasValidIndex_) {
			mapper_->clearMapping();
		}
	}

	hasValidIndex_ = index.isValid();
}

void QtHighlightRuleWidget::onCustomColorsToggled(bool enabled)
{
	if (!enabled) {
		ui_.foreground->setColor(QColor());
		ui_.background->setColor(QColor());
	}
	ui_.foreground->setEnabled(enabled);
	ui_.background->setEnabled(enabled);
}

void QtHighlightRuleWidget::onCustomSoundToggled(bool enabled)
{
	if (enabled) {
		if (ui_.soundFile->text().isEmpty()) {
			onSoundFileButtonClicked();
		}
	} else {
		ui_.soundFile->clear();
	}
	ui_.soundFile->setEnabled(enabled);
	ui_.soundFileButton->setEnabled(enabled);
}

void QtHighlightRuleWidget::onSoundFileButtonClicked()
{
	QString s = QFileDialog::getOpenFileName(this, tr("Choose sound file"), QString(), tr("Sound files (*.wav)"));
	if (!s.isEmpty()) {
		ui_.soundFile->setText(s);
	}
}

void QtHighlightRuleWidget::onHighlightTextToggled(bool enabled)
{
	ui_.customColors->setEnabled(enabled);
}

void QtHighlightRuleWidget::onPlaySoundToggled(bool enabled)
{
	ui_.customSound->setEnabled(enabled);
}

void QtHighlightRuleWidget::save()
{
	if (hasValidIndex_) {
		mapper_->submit();
	}
}

void QtHighlightRuleWidget::revert()
{
	if (hasValidIndex_) {
		mapper_->revert();
	}
}

}