Skip to content

Commit

Permalink
Refactor|libshell: Moved MonospaceLineWrapping to its own source file
Browse files Browse the repository at this point in the history
Also, some minor cleanup.
  • Loading branch information
skyjake committed May 16, 2013
1 parent 379976b commit 1f348fb
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 118 deletions.
1 change: 1 addition & 0 deletions doomsday/libshell/include/de/shell/ILineWrapping
@@ -0,0 +1 @@
#include "libshell.h"
2 changes: 1 addition & 1 deletion doomsday/libshell/include/de/shell/MonospaceLineWrapping
@@ -1 +1 @@
#include "libshell.h"
#include "monospacelinewrapping.h"
2 changes: 2 additions & 0 deletions doomsday/libshell/include/de/shell/abstractlineeditor.h
Expand Up @@ -104,6 +104,8 @@ class LIBSHELL_PUBLIC AbstractLineEditor
void insert(String const &text);

protected:
ILineWrapping &lineWraps();

/// Determines the available maximum width of text lines.
virtual int maximumWidth() const = 0;

Expand Down
29 changes: 0 additions & 29 deletions doomsday/libshell/include/de/shell/libshell.h
Expand Up @@ -19,7 +19,6 @@
#ifndef LIBSHELL_MAIN_H
#define LIBSHELL_MAIN_H

#include <QList>
#include <de/String>

/*
Expand Down Expand Up @@ -75,34 +74,6 @@ class LIBSHELL_PUBLIC ILineWrapping
virtual int height() const = 0;
};

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

Expand Down
2 changes: 0 additions & 2 deletions doomsday/libshell/include/de/shell/lineeditwidget.h
Expand Up @@ -26,8 +26,6 @@
namespace de {
namespace shell {

class Lexicon;

/**
* Widget for word-wrapped text editing.
*
Expand Down
63 changes: 63 additions & 0 deletions doomsday/libshell/include/de/shell/monospacelinewrapping.h
@@ -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
3 changes: 3 additions & 0 deletions doomsday/libshell/libshell.pro
Expand Up @@ -40,6 +40,7 @@ HEADERS += \
include/de/shell/LocalServer \
include/de/shell/LogWidget \
include/de/shell/MenuWidget \
include/de/shell/MonospaceLineWrapping \
include/de/shell/Protocol \
include/de/shell/ServerFinder \
include/de/shell/TextCanvas \
Expand All @@ -63,6 +64,7 @@ HEADERS += \
include/de/shell/localserver.h \
include/de/shell/logwidget.h \
include/de/shell/menuwidget.h \
include/de/shell/monospacelinewrapping.h \
include/de/shell/protocol.h \
include/de/shell/serverfinder.h \
include/de/shell/textcanvas.h \
Expand All @@ -87,6 +89,7 @@ SOURCES += \
src/localserver.cpp \
src/logwidget.cpp \
src/menuwidget.cpp \
src/monospacelinewrapping.cpp \
src/protocol.cpp \
src/serverfinder.cpp \
src/textcanvas.cpp \
Expand Down
7 changes: 6 additions & 1 deletion doomsday/libshell/src/abstractlineeditor.cpp
Expand Up @@ -493,9 +493,14 @@ void AbstractLineEditor::insert(String const &text)
return d->insert(text);
}

ILineWrapping &AbstractLineEditor::lineWraps()
{
return *d->wraps;
}

void AbstractLineEditor::updateLineWraps(LineWrapUpdateBehavior behavior)
{
if(behavior == WrapUnlessWrappedAlready && d->wraps->height())
if(behavior == WrapUnlessWrappedAlready && !d->wraps->isEmpty())
return; // Already wrapped.

d->updateWraps();
Expand Down
85 changes: 0 additions & 85 deletions doomsday/libshell/src/libshell.cpp
Expand Up @@ -22,90 +22,5 @@
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
111 changes: 111 additions & 0 deletions doomsday/libshell/src/monospacelinewrapping.cpp
@@ -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

0 comments on commit 1f348fb

Please sign in to comment.