File: ParagraphIndentSpacing.cpp

package info (click to toggle)
calligra 1%3A2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 290,028 kB
  • sloc: cpp: 1,105,019; xml: 24,940; ansic: 11,807; python: 8,457; perl: 2,792; sh: 1,507; yacc: 1,307; ruby: 1,248; sql: 903; lex: 455; makefile: 89
file content (273 lines) | stat: -rw-r--r-- 11,479 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* This file is part of the KDE project
 * Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org>
 * Copyright (c) 2003 David Faure <faure@kde.org>
 * Copyright (C)  2011 Mojtaba Shahi Senobari <mojtaba.shahi3000@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "ParagraphIndentSpacing.h"

#include <KoParagraphStyle.h>
#include <KDebug>

ParagraphIndentSpacing::ParagraphIndentSpacing(QWidget *parent)
        : QWidget(parent),
        m_fontMetricsChecked(false)
{
    widget.setupUi(this);

    connect(widget.first, SIGNAL(valueChangedPt(qreal)), this, SIGNAL(firstLineMarginChanged(qreal)));
    connect(widget.left, SIGNAL(valueChangedPt(qreal)), this, SIGNAL(leftMarginChanged(qreal)));
    connect(widget.right, SIGNAL(valueChangedPt(qreal)), this, SIGNAL(rightMarginChanged(qreal)));

    // Keep order in sync with lineSpacingType() and display()
    widget.lineSpacing->addItem(i18nc("Line spacing value", "Single"));
    widget.lineSpacing->addItem(i18nc("Line spacing value", "1.5 Lines"));
    widget.lineSpacing->addItem(i18nc("Line spacing value", "Double"));
    widget.lineSpacing->addItem(i18nc("Line spacing type", "Proportional"));    // called Proportional like in OO
    widget.lineSpacing->addItem(i18nc("Line spacing type", "Additional"));    // normal distance + absolute value
    widget.lineSpacing->addItem(i18nc("Line spacing type", "Fixed"));

    connect(widget.first, SIGNAL(valueChangedPt(qreal)), this, SLOT(firstIndenValueChanged()));
    connect(widget.left, SIGNAL(valueChangedPt(qreal)), this, SLOT(leftMarginValueChanged()));
    connect(widget.right, SIGNAL(valueChangedPt(qreal)), this, SLOT(rightMarginValueChanged()));
    connect(widget.after, SIGNAL(valueChangedPt(qreal)), this, SLOT(bottomMarginValueChanged()));
    connect(widget.before, SIGNAL(valueChangedPt(qreal)), this, SLOT(topMarginValueChanged()));
    connect(widget.lineSpacing, SIGNAL(currentIndexChanged(int)), this, SLOT(lineSpacingChanged(int)));
    connect(widget.useFont, SIGNAL(toggled(bool)), this, SLOT(useFontMetrices(bool)));
    connect(widget.autoTextIndent, SIGNAL(stateChanged(int)), this, SLOT(autoTextIndentChanged(int)));
    connect(widget.proportional, SIGNAL(valueChanged(int)), this, SLOT(spacingPercentChanged(int)));
    connect(widget.custom, SIGNAL(valueChangedPt(qreal)), this, SLOT(spacingValueChanged(qreal)));
    lineSpacingChanged(0);
}

void ParagraphIndentSpacing::autoTextIndentChanged(int state)
{
    widget.first->setEnabled(state == Qt::Unchecked);
    m_autoTextIndentInherited = false;
}
void ParagraphIndentSpacing::firstIndenValueChanged()
{
    m_textIndentInherited = false;
}

void ParagraphIndentSpacing::rightMarginValueChanged()
{
    m_rightMarginIngerited = false;
}

void ParagraphIndentSpacing::leftMarginValueChanged()
{
    m_leftMarginInherited = false;
}

void ParagraphIndentSpacing::topMarginValueChanged()
{
    m_topMarginInherited = false;
}

void ParagraphIndentSpacing::bottomMarginValueChanged()
{
    m_bottomMarginInherited = false;
}

void ParagraphIndentSpacing::setDisplay(KoParagraphStyle *style)
{
    m_style = style;
    // TODO : handle relatives
    widget.first->changeValue(style->textIndent());
    widget.left->changeValue(style->leftMargin());
    widget.right->changeValue(style->rightMargin());
    widget.before->changeValue(style->topMargin());
    widget.after->changeValue(style->bottomMargin());

    m_rightMarginIngerited = !style->hasProperty(QTextFormat::BlockRightMargin);
    m_leftMarginInherited = !style->hasProperty(QTextFormat::BlockLeftMargin);
    m_topMarginInherited = !style->hasProperty(QTextFormat::BlockTopMargin);
    m_bottomMarginInherited = !style->hasProperty(QTextFormat::BlockBottomMargin);
    m_autoTextIndentInherited = !style->hasProperty(KoParagraphStyle::AutoTextIndent);
    m_textIndentInherited = !style->hasProperty(QTextFormat::TextIndent);

    widget.autoTextIndent->setChecked(style->autoTextIndent());

    int index;
    if (style->hasProperty(KoParagraphStyle::FixedLineHeight) && style->lineHeightAbsolute() != 0) {
        // this is the strongest; if this is set we don't care what other properties there are.
        index = 5;
    } else if (style->hasProperty(KoParagraphStyle::LineSpacing) && style->lineSpacing() != 0) {
        // if LineSpacing is set then percent is ignored.
        index = 4;
    } else if (style->hasProperty(KoParagraphStyle::PercentLineHeight) && style->lineHeightPercent() != 0) {
        int percent = style->lineHeightPercent();
        if (percent == 120)
            index = 0; // single
        else if (percent == 180)
            index = 1; // 1.5
        else if (percent == 240)
            index = 2; // double
        else
            index = 3; // proportional
    } else {
        index = 0; // nothing set, default is 'single' just like for geeks.
    }
    widget.lineSpacing->setCurrentIndex(index);
    widget.minimumLineSpacing->changeValue(style->minimumLineHeight());
    widget.useFont->setChecked(style->lineSpacingFromFont());
    m_fontMetricsChecked = style->lineSpacingFromFont();
}

void ParagraphIndentSpacing::lineSpacingChanged(int row)
{
    qreal fixedLineHeight = 0, lineSpacing = 0, minimumLineSpacing = 0;
    int percentHeight = 0;
    bool useFontMetrics = (row != 5) && (widget.useFont->isChecked());
    bool percent = false, custom = false;
    qreal customValue = 0.0;
    switch (row) {
        case 0:
            percentHeight = 120;
            minimumLineSpacing = widget.minimumLineSpacing->value();
            break;
        case 1:
            percentHeight = 180;
            minimumLineSpacing = widget.minimumLineSpacing->value();
            break;
        case 2:
            percentHeight = 240;
            minimumLineSpacing = widget.minimumLineSpacing->value();
            break;
        case 3: // proportional
            percent = true;
            widget.proportional->setValue(m_style->lineHeightPercent());
            percentHeight = m_style->lineHeightPercent();
            minimumLineSpacing = widget.minimumLineSpacing->value();
            break;
        case 4: // additional
            custom = true;
            customValue = qMax(qreal(0.1), m_style->lineSpacing());
            lineSpacing = qMax(qreal(0.1), m_style->lineSpacing());
            minimumLineSpacing = widget.minimumLineSpacing->value();
            break;
        case 5: // fixed
            custom = true;
            if (m_style->lineHeightAbsolute() == 0) // unset
                customValue = 12.0; // nice default value...
            else
                customValue = m_style->lineHeightAbsolute();
            fixedLineHeight = customValue;
            break;
        default:; // other cases don't need the spinboxes
    }

    if (custom) {
        widget.custom->setEnabled(true);
        widget.spacingStack->setCurrentWidget(widget.unitsPage);
        widget.custom->changeValue(customValue);
    } else {
        widget.spacingStack->setCurrentWidget(widget.percentPage);
        widget.proportional->setEnabled(percent);
        if (! percent)
            widget.proportional->setValue(100);
    }

    widget.minimumLineSpacing->setEnabled(row != 5);
    widget.useFont->setEnabled(row != 5);
    widget.useFont->setChecked(row == 5 ? false : m_fontMetricsChecked);

    emit lineSpacingChanged(fixedLineHeight, lineSpacing, minimumLineSpacing, percentHeight, useFontMetrics);
}

void ParagraphIndentSpacing::spacingPercentChanged(int percent)
{
    if (widget.lineSpacing->currentIndex() == 3)
        emit lineSpacingChanged(0, 0, (qreal) widget.minimumLineSpacing->value(), percent, widget.useFont->isChecked());
}

void ParagraphIndentSpacing::spacingValueChanged(qreal value)
{
    if (widget.lineSpacing->currentIndex() == 4)
        emit lineSpacingChanged(0, value, (qreal) widget.minimumLineSpacing->value(), 0, widget.useFont->isChecked());
    else if (widget.lineSpacing->currentIndex() == 5)
        emit lineSpacingChanged(value, 0, 0, 0, false);
}

void ParagraphIndentSpacing::save(KoParagraphStyle *style)
{
    // general note; we have to unset values by setting it to zero instead of removing the item
    // since this dialog may be used on a copy style, which will be applied later. And removing
    // items doesn't work for that.
    if (!m_textIndentInherited){
        style->setTextIndent(QTextLength(QTextLength::FixedLength, widget.first->value()));
    }
    if (!m_leftMarginInherited){
        style->setLeftMargin(QTextLength(QTextLength::FixedLength, widget.left->value()));
    }
    if (!m_rightMarginIngerited){
        style->setRightMargin(QTextLength(QTextLength::FixedLength, widget.right->value()));
    }
    if (!m_topMarginInherited){
        style->setTopMargin(QTextLength(QTextLength::FixedLength, widget.before->value()));
    }
    if (!m_bottomMarginInherited){
        style->setBottomMargin(QTextLength(QTextLength::FixedLength, widget.after->value()));
    }
    if (!m_autoTextIndentInherited){
        style->setAutoTextIndent(widget.autoTextIndent->isChecked());
    }
    style->setLineHeightAbsolute(0); // since it trumps percentage based line heights, unset it.
    style->setMinimumLineHeight(QTextLength(QTextLength::FixedLength, 0));
    style->setLineSpacing(0);
    switch (widget.lineSpacing->currentIndex()) {
    case 0: style->setLineHeightPercent(120); break;
    case 1: style->setLineHeightPercent(180); break;
    case 2: style->setLineHeightPercent(240); break;
    case 3: style->setLineHeightPercent(widget.proportional->value()); break;
    case 4:
        if (widget.custom->value() == 0.0) { // then we need to save it differently.
            style->setLineHeightPercent(100);
        } else {
            style->setLineHeightPercent(0);
            style->setLineSpacing(widget.custom->value());
        }
        break;
    case 5: style->setLineHeightPercent(0);
        style->setLineHeightAbsolute(widget.custom->value());
        break;
    }
    if (widget.lineSpacing->currentIndex() != 5)
        style->setMinimumLineHeight(QTextLength(QTextLength::FixedLength, widget.minimumLineSpacing->value()));
    style->setLineSpacingFromFont(widget.lineSpacing->currentIndex() != 5 && widget.useFont->isChecked());
}

void ParagraphIndentSpacing::setUnit(const KoUnit &unit)
{
    widget.first->setUnit(unit);
    widget.left->setUnit(unit);
    widget.right->setUnit(unit);
    widget.before->setUnit(unit);
    widget.after->setUnit(unit);
    widget.custom->setUnit(unit);
    widget.minimumLineSpacing->setUnit(unit);
}

void ParagraphIndentSpacing::useFontMetrices(bool on)
{
    if (widget.lineSpacing->currentIndex() != 5)
        m_fontMetricsChecked = on;
}

#include <ParagraphIndentSpacing.moc>