File: editindentwidget.cpp

package info (click to toggle)
ktikz 0.11~git20161122-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,248 kB
  • ctags: 1,225
  • sloc: cpp: 10,302; xml: 701; sh: 150; makefile: 29
file content (110 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2007, 2011, 2012, 2014 by Glad Deschrijver              *
 *     <glad.deschrijver@gmail.com>                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "editindentwidget.h"

#include <QtGui/QKeyEvent>
#include <QtCore/QSettings>

#include "../common/utils/icon.h"

IndentWidget::IndentWidget(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
	ui.pushButtonClose->setIcon(Icon(QLatin1String("dialog-cancel")));

	readSettings();

	connect(ui.pushButtonIndent, SIGNAL(clicked()), this, SLOT(indent()));
	connect(ui.pushButtonClose, SIGNAL(clicked()), this, SLOT(hide()));
}

IndentWidget::~IndentWidget()
{
}

void IndentWidget::readSettings()
{
	QSettings settings;
	settings.beginGroup(QLatin1String("Editor"));
	ui.radioButtonSpaces->setChecked(settings.value(QLatin1String("Indent/InsertChar"), '\t').toChar() == QLatin1Char(' '));
	ui.radioButtonTabs->setChecked(settings.value(QLatin1String("Indent/InsertChar"), '\t').toChar() != QLatin1Char(' '));
	ui.spinBoxSpaces->setValue(settings.value(QLatin1String("Indent/NumberOfSpaces"), 2).toInt());
	ui.spinBoxTabs->setValue(settings.value(QLatin1String("Indent/NumberOfTabs"), 1).toInt());
	settings.endGroup();
}

void IndentWidget::writeSettings()
{
	QSettings settings;
	settings.beginGroup(QLatin1String("Editor"));
	settings.setValue(QLatin1String("Indent/InsertChar"), ui.radioButtonSpaces->isChecked() ? QVariant::fromValue(' ') : QVariant::fromValue('\t'));
	settings.setValue(QLatin1String("Indent/NumberOfSpaces"), ui.spinBoxSpaces->value());
	settings.setValue(QLatin1String("Indent/NumberOfTabs"), ui.spinBoxTabs->value());
	settings.endGroup();
}

void IndentWidget::setUnindenting(bool isUnindenting)
{
	m_isUnindenting = isUnindenting;
	setWindowTitle(m_isUnindenting ? tr("Unindent") : tr("Indent"));
	ui.pushButtonIndent->setText(m_isUnindenting ? tr("Unin&dent") : tr("In&dent"));
}

QChar IndentWidget::insertChar() const
{
	return ui.radioButtonSpaces->isChecked() ? QLatin1Char(' ') : QLatin1Char('\t');
}

int IndentWidget::numOfInserts() const
{
	return ui.radioButtonSpaces->isChecked() ? ui.spinBoxSpaces->value() : ui.spinBoxTabs->value();
}

void IndentWidget::indent()
{
	const QChar insertChar = ui.radioButtonSpaces->isChecked() ? QLatin1Char(' ') : QLatin1Char('\t');
	const int numOfInserts = ui.radioButtonSpaces->isChecked() ? ui.spinBoxSpaces->value() : ui.spinBoxTabs->value();
	Q_EMIT indent(insertChar, numOfInserts, m_isUnindenting);
	writeSettings();
}

void IndentWidget::hide()
{
	setVisible(false);
	Q_EMIT hidden();
	readSettings();
}

void IndentWidget::showEvent(QShowEvent *event)
{
	if (ui.radioButtonSpaces->isChecked())
		setFocusProxy(ui.radioButtonSpaces);
	else
		setFocusProxy(ui.radioButtonTabs);
	QWidget::showEvent(event);
}

void IndentWidget::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Escape)
		hide();
	else if (event->key() == Qt::Key_Return)
		indent();
	QWidget::keyPressEvent(event);
}