1 /* This file is part of the KDE project
2  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2009 Pierre Stirnweiss <pstirnweiss@googlemail.com>
4  * Copyright (C) 2012 Gopalakrishna Bhat A <gopalakbhat@gmail.com>
5  * Copyright (C) 2012 Mojtaba Shahi Senobari <mojtaba.shahi3000@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "CharacterGeneral.h"
24 #include "CharacterHighlighting.h"
25 #include "LanguageTab.h"
26 #include "FontDecorations.h"
27 #include "FormattingPreview.h"
28 
29 #include "StylesCombo.h"
30 #include "StylesModel.h"
31 
32 #include <KoParagraphStyle.h>
33 #include <KoStyleThumbnailer.h>
34 #include <KoStyleManager.h>
35 #include <KoCharacterStyle.h>
36 
37 #include <QDebug>
38 
CharacterGeneral(QWidget * parent)39 CharacterGeneral::CharacterGeneral(QWidget *parent)
40         : QWidget(parent)
41         , m_style(0)
42         , m_styleManager(0)
43         , m_thumbnail(new KoStyleThumbnailer())
44         , m_paragraphStyleModel(new StylesModel(0,StylesModel::ParagraphStyle))
45         , m_characterInheritedStyleModel(new StylesModel(0, StylesModel::CharacterStyle))
46 {
47     widget.setupUi(this);
48     // we don't have next style for character styles
49     widget.nextStyle->setVisible(false);
50     widget.label_2->setVisible(false);
51     //
52 
53     // paragraph style model
54     widget.nextStyle->showEditIcon(false);
55     widget.nextStyle->setStyleIsOriginal(true);
56     m_paragraphStyleModel->setStyleThumbnailer(m_thumbnail);
57     widget.nextStyle->setStylesModel(m_paragraphStyleModel);
58     // inherited style model
59     widget.inheritStyle->showEditIcon(false);
60     widget.inheritStyle->setStyleIsOriginal(true);
61     //for character General
62     m_characterInheritedStyleModel->setStyleThumbnailer(m_thumbnail);
63     widget.inheritStyle->setStylesModel(m_characterInheritedStyleModel);
64     widget.inheritStyle->setEnabled(false);
65 
66     m_characterHighlighting = new CharacterHighlighting(this);
67     connect(m_characterHighlighting, SIGNAL(charStyleChanged()), this, SIGNAL(styleChanged()));
68     connect(m_characterHighlighting, SIGNAL(charStyleChanged()), this, SLOT(setPreviewCharacterStyle()));
69 
70     m_languageTab = new LanguageTab(true, this);
71 
72     widget.tabs->addTab(m_characterHighlighting, i18n("Font"));
73 
74     m_languageTab->setVisible(false);
75 
76     connect(widget.name, SIGNAL(textChanged(QString)), this, SIGNAL(nameChanged(QString)));
77 }
78 
hideStyleName(bool hide)79 void CharacterGeneral::hideStyleName(bool hide)
80 {
81     if (hide) {
82         disconnect(widget.name, SIGNAL(textChanged(QString)), this, SIGNAL(nameChanged(QString)));
83         widget.tabs->removeTab(0);
84         m_nameHidden = true;
85     }
86 }
87 
setStyle(KoCharacterStyle * style,bool directFormattingMode)88 void CharacterGeneral::setStyle(KoCharacterStyle *style, bool directFormattingMode)
89 {
90     m_style = style;
91     if (m_style == 0)
92         return;
93     blockSignals(true);
94 
95     if (!m_nameHidden)
96         widget.name->setText(style->name());
97 
98     m_characterHighlighting->setDisplay(style, directFormattingMode);
99     //m_languageTab->setDisplay(style);
100 
101     widget.preview->setCharacterStyle(style);
102 
103     if (m_styleManager) {
104         KoCharacterStyle *parentStyle = style->parentStyle();
105         if (parentStyle) {
106             widget.inheritStyle->setCurrentIndex(m_characterInheritedStyleModel->indexOf(parentStyle).row());
107         }
108     }
109 
110     blockSignals(false);
111 }
112 
save(KoCharacterStyle * style)113 void CharacterGeneral::save(KoCharacterStyle *style)
114 {
115     KoCharacterStyle *savingStyle;
116     if (style == 0) {
117         if (m_style == 0)
118             return;
119         else
120             savingStyle = m_style;
121     }
122     else
123         savingStyle = style;
124 
125     m_characterHighlighting->save(savingStyle);
126     //m_languageTab->save(savingStyle);
127     savingStyle->setName(widget.name->text());
128 
129     if (m_style == savingStyle) {
130         emit styleAltered(savingStyle);
131     }
132 }
133 
switchToGeneralTab()134 void CharacterGeneral::switchToGeneralTab()
135 {
136     widget.tabs->setCurrentIndex(0);
137 }
138 
selectName()139 void CharacterGeneral::selectName()
140 {
141     widget.tabs->setCurrentIndex(widget.tabs->indexOf(widget.generalTab));
142     widget.name->selectAll();
143     widget.name->setFocus(Qt::OtherFocusReason);
144 }
145 
setPreviewCharacterStyle()146 void CharacterGeneral::setPreviewCharacterStyle()
147 {
148     KoCharacterStyle *charStyle = new KoCharacterStyle();
149     save(charStyle);
150     if (charStyle) {
151         widget.preview->setCharacterStyle(charStyle);
152     }
153 
154     delete charStyle;
155 }
156 
styleName() const157 QString CharacterGeneral::styleName() const
158 {
159     return widget.name->text();
160 }
161 
setStyleManager(KoStyleManager * sm)162 void CharacterGeneral::setStyleManager(KoStyleManager *sm)
163 {
164     if (!sm)
165         return;
166     m_styleManager = sm;
167     m_paragraphStyleModel->setStyleManager(m_styleManager);
168     m_characterInheritedStyleModel->setStyleManager(m_styleManager);
169 }
170 
updateNextStyleCombo(KoParagraphStyle * style)171 void CharacterGeneral::updateNextStyleCombo(KoParagraphStyle *style)
172 {
173     if (!style)
174         return;
175 
176     widget.nextStyle->setCurrentIndex(m_paragraphStyleModel->indexOf(style).row());
177     m_paragraphStyleModel->setCurrentParagraphStyle(style->styleId());
178 }
179 
nextStyleId()180 int CharacterGeneral::nextStyleId()
181 {
182     if (!m_styleManager) {
183         return 0;
184     }
185     int nextStyleIndex = widget.nextStyle->currentIndex();
186     QModelIndex paragraphStyleIndex = m_paragraphStyleModel->index(nextStyleIndex);
187     quint64 internalId = paragraphStyleIndex.internalId();
188     KoParagraphStyle * paragraphStyle = m_styleManager->paragraphStyle(internalId);
189     if (paragraphStyle) {
190         return paragraphStyle->styleId();
191     }
192     else {
193         return 0;
194     }
195 }
196 
style() const197 KoCharacterStyle *CharacterGeneral::style() const
198 {
199     return m_style;
200 }
201