lyx_mirror/src/frontends/qt4/qt_helpers.cpp

216 lines
4.7 KiB
C++
Raw Normal View History

/**
* \file qt_helpers.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Dekel Tsur
* \author J<EFBFBD>rgen Spitzm<EFBFBD>ller
* \author Richard Heck
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "LengthCombo.h"
#include "qt_helpers.h"
#include "lengthcommon.h"
#include "gettext.h"
#include "support/os.h"
#include "support/lstrings.h"
#include "support/convert.h"
#include "debug.h"
#include <QComboBox>
#include <QCheckBox>
#include <qlineedit.h>
#include <qtextcodec.h>
#include <algorithm>
namespace lyx {
Make libQtCore a support library like boost and implement encoding conversion from/to the local 8bit encoding with it. Only the autotools build system is updated, scons and cmake users need to add qt4 cpp flags when compiling libsupport, and link libsupport against libQtCore. * src/frontends/qt4/qt_helpers.[Ch] (toqstr, qchar_to_ucs4, ucs4_to_qchar, ucs4_to_qstring, qstring_to_ucs4, fromqstr): Move these qstring conversion functions from here ... * src/support/qstring_helpers.[Ch] ... to these new files * src/support/docstring.[Ch] (from_local8bit): new conversion function from local 8bit encoding to ucs4 (to_local8bit): new conversion function from ucs4 to local 8bit encoding to ucs4 (to_local8bit_failure): exception that is thrown by to_local8bit if the argument cannot be converted to the local encoding * src/support/filename.C (FileName::toFilesystemEncoding): implement with the help of QFile * src/support/Makefile.am: Add new files, qt4 cpp flags and link against libQtCore * src/client/client.C: Convert commandline input from local encoding to ucs4. Convert stuff that is sent to to the server to utf8, because LyX interprets it as utf8 on the other end of the pipe. * src/lyx_main.C (LyX::exec): convert commandline input from local encoding to utf8 (LyX::init): ditto (LyX::easyParse): ditto * development/scons/scons_manifest.py: Add new files * config/qt4.m4: Define new variables QT4_CORE_INCLUDES, QT4_CORE_LDFLAGS and QT4_CORE_LIB git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16257 a592a061-630c-0410-9148-cb99ea01b6c8
2006-12-12 20:19:46 +00:00
using support::isStrDbl;
using std::vector;
using std::make_pair;
using std::string;
using std::pair;
using std::endl;
string makeFontName(string const & family, string const & foundry)
{
if (foundry.empty())
return family;
return family + " [" + foundry + ']';
}
pair<string, string> parseFontName(string const & name)
{
string::size_type const idx = name.find('[');
if (idx == string::npos || idx == 0)
return make_pair(name, string());
return make_pair(name.substr(0, idx - 1),
name.substr(idx + 1, name.size() - idx - 2));
}
string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
{
QString const length = input->text();
if (length.isEmpty())
return string();
// Don't return unit-from-choice if the input(field) contains a unit
if (isValidGlueLength(fromqstr(length)))
return fromqstr(length);
LyXLength::UNIT const unit = combo->currentLengthItem();
return LyXLength(length.toDouble(), unit).asString();
}
LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
{
QString const length = input->text();
if (length.isEmpty())
return LyXLength();
// don't return unit-from-choice if the input(field) contains a unit
if (isValidGlueLength(fromqstr(length)))
return LyXLength(fromqstr(length));
LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
return LyXLength(length.toDouble(), unit);
}
void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
LyXLength const & len, LyXLength::UNIT defaultUnit)
{
combo->setCurrentItem(LyXLength(len).unit());
input->setText(toqstr(convert<string>(LyXLength(len).value())));
}
void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
string const & len, LyXLength::UNIT defaultUnit)
{
if (len.empty()) {
// no length (UNIT_NONE)
combo->setCurrentItem(defaultUnit);
input->setText("");
} else if (!isValidLength(len) && !isStrDbl(len)) {
// use input field only for gluelengths
combo->setCurrentItem(defaultUnit);
input->setText(toqstr(len));
} else {
lengthToWidgets(input, combo, LyXLength(len), defaultUnit);
}
}
void lengthAutoToWidgets(QLineEdit * input, LengthCombo * combo,
LyXLength const & len, LyXLength::UNIT defaultUnit)
{
if (len.value() == 0)
lengthToWidgets(input, combo, "auto", defaultUnit);
else
lengthToWidgets(input, combo, len, defaultUnit);
}
//NOTE "CB" here because we probably will want one of these
//for labeled sets, as well.
void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
LengthCombo * lengthCombo)
{
if (!checkBox->isChecked())
lengthToWidgets(lineEdit, lengthCombo,
"auto", lengthCombo->currentLengthItem());
else if (lineEdit->text() == "auto")
lengthToWidgets(lineEdit, lengthCombo, string(),
lengthCombo->currentLengthItem());
}
QString const qt_(char const * str, const char *)
{
return toqstr(_(str));
}
QString const qt_(string const & str)
{
return toqstr(_(str));
}
docstring const formatted(docstring const & text, int w)
{
docstring sout;
if (text.empty())
return sout;
docstring::size_type curpos = 0;
docstring line;
for (;;) {
docstring::size_type const nxtpos1 = text.find(' ', curpos);
docstring::size_type const nxtpos2 = text.find('\n', curpos);
docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
docstring const word =
nxtpos == docstring::npos ?
text.substr(curpos) :
text.substr(curpos, nxtpos - curpos);
bool const newline = (nxtpos2 != docstring::npos &&
nxtpos2 < nxtpos1);
docstring const line_plus_word =
line.empty() ? word : line + char_type(' ') + word;
// FIXME: make w be size_t
if (int(line_plus_word.length()) >= w) {
sout += line + char_type('\n');
if (newline) {
sout += word + char_type('\n');
line.erase();
} else {
line = word;
}
} else if (newline) {
sout += line_plus_word + char_type('\n');
line.erase();
} else {
if (!line.empty())
line += char_type(' ');
line += word;
}
if (nxtpos == docstring::npos) {
if (!line.empty())
sout += line;
break;
}
curpos = nxtpos + 1;
}
return sout;
}
} // namespace lyx