Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor|libshell: Moved MonospaceLineWrapping to its own source file
Also, some minor cleanup.
- Loading branch information
Showing
10 changed files
with
187 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "libshell.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
#include "libshell.h" | ||
#include "monospacelinewrapping.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,6 @@ | |
namespace de { | ||
namespace shell { | ||
|
||
class Lexicon; | ||
|
||
/** | ||
* Widget for word-wrapped text editing. | ||
* | ||
|
63 changes: 63 additions & 0 deletions
63
doomsday/libshell/include/de/shell/monospacelinewrapping.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** @file monospacelinewrapping.h | ||
* | ||
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>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</small> | ||
*/ | ||
|
||
#ifndef LIBSHELL_MONOSPACELINEWRAPPING_H | ||
#define LIBSHELL_MONOSPACELINEWRAPPING_H | ||
|
||
#include "libshell.h" | ||
#include <QList> | ||
|
||
namespace de { | ||
namespace shell { | ||
|
||
/** | ||
* Line wrapper that assumes that all characters are the same width. Width is | ||
* defined in characters, height in lines. | ||
*/ | ||
class LIBSHELL_PUBLIC MonospaceLineWrapping : public ILineWrapping | ||
{ | ||
public: | ||
MonospaceLineWrapping(); | ||
|
||
bool isEmpty() const; | ||
|
||
void clear(); | ||
|
||
/** | ||
* Determines word wrapping for a line of text given a maximum line width. | ||
* | ||
* @param text Text to wrap. | ||
* @param maxWidth Maximum width for each text line. | ||
* | ||
* @return List of positions in @a text where to break the lines. Total number | ||
* of word-wrapped lines is equal to the size of the returned list. | ||
*/ | ||
void wrapTextToWidth(String const &text, int maxWidth); | ||
|
||
WrappedLine line(int index) const { return _lines[index]; } | ||
int width() const; | ||
int height() const; | ||
|
||
private: | ||
QList<WrappedLine> _lines; | ||
}; | ||
|
||
} // namespace shell | ||
} // namespace de | ||
|
||
#endif // LIBSHELL_MONOSPACELINEWRAPPING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** @file monospacelinewrapping.cpp | ||
* | ||
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>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</small> | ||
*/ | ||
|
||
#include "de/shell/MonospaceLineWrapping" | ||
#include <de/math.h> | ||
|
||
namespace de { | ||
namespace shell { | ||
|
||
MonospaceLineWrapping::MonospaceLineWrapping() | ||
{ | ||
//_lines.append(WrappedLine(Range(), true)); | ||
} | ||
|
||
bool MonospaceLineWrapping::isEmpty() const | ||
{ | ||
return _lines.isEmpty(); | ||
} | ||
|
||
void MonospaceLineWrapping::clear() | ||
{ | ||
_lines.clear(); | ||
} | ||
|
||
void MonospaceLineWrapping::wrapTextToWidth(String const &text, int maxWidth) | ||
{ | ||
QChar const newline('\n'); | ||
|
||
clear(); | ||
|
||
if(maxWidth < 1) return; // No room to wrap. | ||
|
||
int const lineWidth = maxWidth; | ||
int begin = 0; | ||
forever | ||
{ | ||
// Newlines always cause a wrap. | ||
int end = begin; | ||
while(end < begin + lineWidth && | ||
end < text.size() && text.at(end) != newline) ++end; | ||
|
||
if(end == text.size()) | ||
{ | ||
// Time to stop. | ||
_lines.append(WrappedLine(Range(begin, text.size()))); | ||
break; | ||
} | ||
|
||
// Find a good break point. | ||
while(!text.at(end).isSpace()) | ||
{ | ||
--end; | ||
if(end == begin) | ||
{ | ||
// Ran out of non-space chars, force a break. | ||
end = begin + lineWidth; | ||
break; | ||
} | ||
} | ||
|
||
if(text.at(end) == newline) | ||
{ | ||
// The newline will be omitted from the wrapped lines. | ||
_lines.append(WrappedLine(Range(begin, end))); | ||
begin = end + 1; | ||
} | ||
else | ||
{ | ||
if(text.at(end).isSpace()) ++end; | ||
_lines.append(WrappedLine(Range(begin, end))); | ||
begin = end; | ||
} | ||
} | ||
|
||
// Mark the final line. | ||
_lines.last().isFinal = true; | ||
} | ||
|
||
int MonospaceLineWrapping::width() const | ||
{ | ||
int w = 0; | ||
for(int i = 0; i < _lines.size(); ++i) | ||
{ | ||
WrappedLine const &span = _lines[i]; | ||
w = de::max(w, span.range.size()); | ||
} | ||
return w; | ||
} | ||
|
||
int MonospaceLineWrapping::height() const | ||
{ | ||
return _lines.size(); | ||
} | ||
|
||
} // namespace shell | ||
} // namespace de |