ImportStyle.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Nicolas GOUTTE <goutte@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqfontinfo.h>
21
22#include <tdeglobalsettings.h>
23#include <kdebug.h>
24
25#include <KoGlobal.h>
26
27#include "ImportStyle.h"
28
29StyleData::StyleData(void): m_level(-1)
30{
31}
32
33StyleDataMap::StyleDataMap(void)
34{
35}
36
37TQString StyleDataMap::getDefaultStyle(void)
38{
39 // We use TQFontInfo, as it return real values
40 TQFontInfo fontInfo(KoGlobal::defaultFont());
41 TQString strReturn;
42
43 strReturn += "font-family:";
44 strReturn += fontInfo.family(); // TODO: should be "Times New Roman"
45 strReturn += "; font-size: 12pt;";
46 // Note: the last property must have a semi-colon!
47
48 return strReturn;
49}
50
51void StyleDataMap::defineNewStyleFromOld(const TQString& strName, const TQString& strOld,
52 const int level, const TQString& strProps)
53{
54 if (strOld.isEmpty())
55 {
56 defineNewStyle(strName,level,strProps);
57 return;
58 }
59
60 StyleDataMap::Iterator it=find(strOld);
61 if (it==end())
62 {
63 defineNewStyle(strName,level,strProps);
64 }
65 else
66 {
67 TQString strAllProps=it.data().m_props;
68 strAllProps+=strProps;
69 defineNewStyle(strName,level,strAllProps);
70 }
71}
72
73
74void StyleDataMap::defineNewStyle(const TQString& strName, const int level,
75 const TQString& strProps)
76{
77 // Despite its name, this method can be called multiple times
78 // We must take care that KWord just gets it only one time.
79 StyleDataMap::Iterator it=find(strName);
80 if (it==end())
81 {
82 // The style does not exist yet, so we must define it.
83 it=insert(strName,StyleData());
84 }
85 StyleData& styleData=it.data();
86 styleData.m_level=level;
87 styleData.m_props+=getDefaultStyle();
88 if (!strProps.isEmpty())
89 {
90 styleData.m_props+=strProps;
91 styleData.m_props+=";"; // Security if other properties are appended later
92 }
93}
94
95StyleDataMap::Iterator StyleDataMap::useOrCreateStyle(const TQString& strName)
96{
97 // We are using a style but we are not sure if it is defined
98 StyleDataMap::Iterator it=find(strName);
99 if (it==end())
100 {
101 // The style is not yet defined!
102 StyleData data;
103 data.m_level=-1;
104 data.m_props=getDefaultStyle();
105 it=insert(strName,data);
106 }
107 return it;
108}
109
110void StyleDataMap::defineDefaultStyles(void)
111{
112 // Add a few of AbiWord predefined style sheets
113 // AbiWord file: src/text/ptbl/xp/pt_PT_Styles.cpp
114 defineNewStyle("Normal",-1,TQString());
115 // TODO: font should be "Arial"
116 // TODO: "keep with next"
117 TQString strHeading("font-weight: bold; margin-top: 22pt; margin-bottom: 3pt; ");
118 defineNewStyle("Heading 1",1,strHeading+"font-size: 17pt");
119 defineNewStyle("Heading 2",2,strHeading+"font-size: 14pt");
120 defineNewStyle("Heading 3",3,strHeading+"font-size: 12pt");
121 defineNewStyle("Block Text",-1,"margin-left: 1in; margin-right: 1in; margin-bottom: 6pt");
122 TQFontInfo fixedInfo(TDEGlobalSettings::fixedFont());
123 TQString strPlainText=TQString("font-family: %1")
124 .arg(fixedInfo.family()); // TODO: should be "Courier New"
125 kdDebug(30506) << "Plain Text: " << strPlainText << endl;
126 defineNewStyle("Plain Text",-1,strPlainText);
127 // TODO: all list and numbered types
128}