Scribus
Open source desktop publishing at your fingertips
storytext.h
1 /*
2  For general Scribus (>=1.3.2) copyright and licensing information please refer
3  to the COPYING file provided with the program. Following this notice may exist
4  a copyright and/or license notice that predates the release of Scribus 1.3.2
5  for which a new license (GPL+exception) is in place.
6  */
7 /***************************************************************************
8 pageitem.cpp - description
9 -------------------
10  begin : Sat Apr 7 2001
11  copyright : (C) 2001 by Franz Schmid
12  email : Franz.Schmid@altmuehlnet.de
13  ***************************************************************************/
14 
15 /***************************************************************************
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 ***************************************************************************/
23 
24 
25 #ifndef STORYTEXT_H_
26 #define STORYTEXT_H_
27 
28 #include <QObject>
29 #include <QString>
30 #include <QList>
31 #include <cassert>
32 
33 #include "marks.h"
34 //#include "text/paragraphlayout.h"
35 #include "text/frect.h"
36 #include "text/specialchars.h"
37 #include "sctextstruct.h"
38 #include "style.h"
39 #include "styles/charstyle.h"
40 #include "styles/paragraphstyle.h"
41 #include "desaxe/saxio.h"
42 
43 
44 class CharStyle;
45 class ParagraphStyle;
46 class PageItem;
47 //class ScTextEngine;
48 //class ScScriptItem;
49 class ScribusDoc;
50 class ScText_Shared;
51 class ResourceCollection;
52 
53 
54 
71 class SCRIBUS_API StoryText : public QObject, public SaxIO
72 {
73  Q_OBJECT
74 
75  public:
76  StoryText(ScribusDoc *doc);
77  StoryText();
78  StoryText(const StoryText & other);
79  StoryText& operator= (const StoryText & other);
80  virtual ~StoryText();
81  void setDoc(ScribusDoc *docin);
82 
83  static const Xml_string saxxDefaultElem;
84  static void desaxeRules(const Xml_string& prefixPattern, desaxe::Digester& ruleset, Xml_string elemtag = saxxDefaultElem);
85 
86  virtual void saxx(SaxHandler& handler, const Xml_string& elemtag) const;
87  virtual void saxx(SaxHandler& handler) const { saxx(handler, saxxDefaultElem); }
88 
89  int cursorPosition() const;
90  void setCursorPosition(int pos, bool relative = false);
91  void normalizeCursorPosition();
92  int normalizedCursorPosition();
93 
94  void clear();
95  StoryText copy() const;
96 
97  // Find text in story
98  int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
99  int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
100 
101  // Add, change, replace
102  // Insert chars from another StoryText object at current cursor position
103  void insert(const StoryText& other, bool onlySelection = false);
104  // Insert chars from another StoryText object at specific position
105  void insert(int pos, const StoryText& other, bool onlySelection = false);
106  // Append chars from another StoryText object
107  void append(const StoryText& other) { insert(length(), other, false); }
108  // Remove len chars at specific position
109  void removeChars(int pos, uint len);
110  // Removes trailing empty paragraphs
111  void trim();
112  // Insert chars at current cursor position
113  void insertChars(QString txt, bool applyNeighbourStyle = false);
114  // Insert chars ar specific position
115  void insertChars(int pos, QString txt, bool applyNeighbourStyle = false);
116  // Insert inline object at current cursor position
117  void insertObject(int obj);
118  // Insert object at specific position
119  void insertObject(int pos, int obj);
120  // Insert mark at cursor or specific position
121  void insertMark(Mark* Mark, int pos = -1);
122  void replaceChar(int pos, QChar ch);
123  // Replaced a word, and return the difference in length between old and new
124  int replaceWord(int pos, QString newWord);
125  void replaceObject(int pos, int obj);
126 
127  void hyphenateWord(int pos, uint len, char* hyphens);
128 
129  // Retrieve length of story text
130  int length() const;
131 
132  // Get content at specific position as plain text
133  // Internal paragraph separator are converted to
134  // unix new lines for better compatibility with
135  // text editors
136  QString plainText() const;
137 
138  // Get char at current cursor position
139  QChar text() const;
140  // Get char at specific position
141  QChar text(int pos) const;
142  // Get text with len chars at specific position
143  QString text(int pos, uint len) const;
144  //Get sentence at any position within it
145  QString sentence(int pos, int &posn);
146  // Get word at specific position
147  QString wordAt(int pos) const;
148 
149  bool hasObject(int pos) const;
150  PageItem* object(int pos) const;
151  bool hasMark(int pos, Mark* mrk = NULL) const;
152  Mark *mark(int pos) const;
153  void replaceMark(int pos, Mark* mrk);
154 
155  // Get charstyle at current cursor position
156  const CharStyle& charStyle() const;
157  // Get charstyle at specific position
158  const CharStyle& charStyle(int pos) const;
159  // Get paragraph style at current cursor position
160  const ParagraphStyle& paragraphStyle() const;
161  // Get paragraph style at specific position
162  const ParagraphStyle& paragraphStyle(int pos) const;
163  const ParagraphStyle& defaultStyle() const;
164  void setDefaultStyle(const ParagraphStyle& style);
165  void setCharStyle(int pos, uint len, const CharStyle& style);
166  void setStyle(int pos, const ParagraphStyle& style);
167  void applyCharStyle(int pos, uint len, const CharStyle& style);
168  void applyStyle(int pos, const ParagraphStyle& style, bool rmDirectFormatting = false);
169  void eraseCharStyle(int pos, uint len, const CharStyle& style);
170  void eraseStyle(int pos, const ParagraphStyle& style);
171  void replaceStyles(QMap<QString,QString> newNameForOld);
172  void replaceCharStyles(QMap<QString,QString> newNameForOld);
173 
174  // Cleanup legacy formatting for whole story, ie remove direct
175  // formatting for parameters already set at paragraph level
176  void fixLegacyFormatting();
177 
178  // Cleanup legacy formatting for paragraph at position pos
179  void fixLegacyFormatting(int pos);
180 
181  void getNamedResources(ResourceCollection& lists) const;
182  void replaceNamedResources(ResourceCollection& newNames);
183 
184  uint nrOfParagraphs() const;
185  int startOfParagraph() const;
186  int startOfParagraph(uint index) const;
187  int endOfParagraph() const;
188  int endOfParagraph(uint index) const;
189  uint nrOfParagraph() const;
190  uint nrOfParagraph(int pos) const;
191 
192  uint nrOfRuns() const;
193  int startOfRun(uint index) const;
194  int endOfRun(uint index) const;
195 
196 // positioning
197  int nextChar(int pos);
198  int prevChar(int pos);
199  int firstWord();
200  int nextWord(int pos);
201  int prevWord(int pos);
202  int endOfWord(int pos) const;
203  int nextSentence(int pos);
204  int prevSentence(int pos);
205  int nextParagraph(int pos);
206  int prevParagraph(int pos);
207 
208 // these need valid layout:
209 
210 // int startOfLine(int pos);
211 // int endOfLine(int pos);
212 // int prevLine(int pos);
213 // int nextLine(int pos);
214 // int startOfFrame(int pos);
215 // int endOfFrame(int pos);
216 
217 // selection
218 
219  void selectAll();
220  void deselectAll();
221  void removeSelection();
222  void extendSelection(int oldPos, int newPos);
223  int selectWord(int pos);
224  void select(int pos, uint len, bool on = true);
225  bool selected(int pos) const;
226  int startOfSelection() const;
227  int endOfSelection() const;
228  int lengthOfSelection() const;
229 
230 
231 // layout helpers
232 
233  GlyphLayout* getGlyphs(int pos);
234  const GlyphLayout* getGlyphs(int pos) const;
235  LayoutFlags flags(int pos) const;
236  bool hasFlag(int pos, LayoutFlags flag) const;
237  void setFlag(int pos, LayoutFlags flag);
238  void clearFlag(int pos, LayoutFlags flag);
239 
240 // when physical view doesn't match logical view any more:
241 
243  void invalidateObject(const PageItem* embedded);
245  void invalidateLayout();
246 
247 public slots:
249  void invalidateAll();
250 
251 signals:
252  void changed();
253 
254 private:
255  ScText * item(uint index);
256  const ScText * item(uint index) const;
257 
258 //public:
259 // ScText * item_p(uint index) { return item(index); }
260 
261 // int screenToPosition(FPoint coord) const;
262 // FRect boundingBox(int pos, uint len = 1) const;
263 
264 // uint lines() const { return (uint) m_lines.count(); }
265 
266 // LineSpec line(uint i) const { return m_lines[i]; }
267 
268 
269 private:
270  ScribusDoc * m_doc;
271  int m_selFirst, m_selLast;
272 // int m_firstFrameItem, m_lastFrameItem;
273 // QList<LineSpec> m_lines;
274 // bool m_validLayout;
275 // qreal m_magicX;
276 // int m_lastMagicPos;
277 
278  QString textWithSoftHyphens (int pos, uint len) const;
279  void insertCharsWithSoftHyphens(int pos, QString txt, bool applyNeighbourStyle = false);
280 
282  void invalidate(int firstRun, int lastRun);
283  void removeParSep(int pos);
284  void insertParSep(int pos);
285 
286  // int splitRun(int pos);
287 
291 // void validate();
293  ScText_Shared * d;
295 // uint layouterVersion;
297 // bool layouterValid;
298  };
299 
300 
301 #endif /*STORYTEXT_H_*/
Definition: storytext.h:71
Definition: charstyle.h:78
Definition: saxhandler.h:21
Definition: sctextstruct.h:109
Definition: paragraphstyle.h:27
Definition: marks.h:40
virtual void saxx(SaxHandler &handler) const
Definition: storytext.h:87
the Document Class
Definition: scribusdoc.h:90
Definition: sctext_shared.h:17
static const Xml_string saxxDefaultElem
Definition: saxio.h:40
Definition: saxio.h:26
Definition: pageitem.h:92
static void desaxeRules(const Xml_string &, desaxe::Digester &, const Xml_string=saxxDefaultElem)
Definition: saxio.h:49
Definition: digester.h:100
virtual void saxx(SaxHandler &, const Xml_string &) const =0
Definition: resourcecollection.h:41
Definition: sctextstruct.h:54