move the string conversion functions out-of-line to minimize #includes

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24396 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2008-04-20 20:32:00 +00:00
parent aff97fe9b5
commit 1e4f4fcd91
9 changed files with 141 additions and 49 deletions

View File

@ -80,9 +80,10 @@ void FileDialog::setButton2(QString const & label, QString const & dir)
FileDialog::Result FileDialog::save(QString const & path, FileDialog::Result FileDialog::save(QString const & path,
QStringList const & filters, QString const & suggested) QStringList const & filters, QString const & suggested)
{ {
LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) LYXERR(Debug::GUI, "Select with path \"" << path
<< "\", mask \"" << fromqstr(filters.join(";;")) << "\", mask \"" << filters.join(";;")
<< "\", suggested \"" << fromqstr(suggested) << '"'); << "\", suggested \"" << suggested << '"');
FileDialog::Result result; FileDialog::Result result;
result.first = FileDialog::Chosen; result.first = FileDialog::Chosen;
@ -117,9 +118,9 @@ FileDialog::Result FileDialog::save(QString const & path,
FileDialog::Result FileDialog::open(QString const & path, FileDialog::Result FileDialog::open(QString const & path,
QStringList const & filters, QString const & suggested) QStringList const & filters, QString const & suggested)
{ {
LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) LYXERR(Debug::GUI, "Select with path \"" << path
<< "\", mask \"" << fromqstr(filters.join(";;")) << "\", mask \"" << filters.join(";;")
<< "\", suggested \"" << fromqstr(suggested) << '"'); << "\", suggested \"" << suggested << '"');
FileDialog::Result result; FileDialog::Result result;
result.first = FileDialog::Chosen; result.first = FileDialog::Chosen;
@ -148,8 +149,8 @@ FileDialog::Result FileDialog::open(QString const & path,
FileDialog::Result FileDialog::opendir(QString const & path, FileDialog::Result FileDialog::opendir(QString const & path,
QString const & suggested) QString const & suggested)
{ {
LYXERR(Debug::GUI, "Select with path \"" << fromqstr(path) LYXERR(Debug::GUI, "Select with path \"" << path
<< "\", suggested \"" << fromqstr(suggested) << '"'); << "\", suggested \"" << suggested << '"');
FileDialog::Result result; FileDialog::Result result;
result.first = FileDialog::Chosen; result.first = FileDialog::Chosen;

View File

@ -223,8 +223,8 @@ void GuiCharacter::change_adaptor()
} }
template<class B> template<class P, class B>
static int findPos2nd(QList<pair<QString, B> > const & vec, B const & val) static int findPos2nd(QList<P> const & vec, B const & val)
{ {
for (int i = 0; i != vec.size(); ++i) for (int i = 0; i != vec.size(); ++i)
if (vec[i].second == val) if (vec[i].second == val)

View File

@ -19,6 +19,8 @@
#include "qt_helpers.h" // for LanguagePair #include "qt_helpers.h" // for LanguagePair
#include "Font.h" #include "Font.h"
#include <utility>
namespace lyx { namespace lyx {
namespace frontend { namespace frontend {

View File

@ -88,7 +88,7 @@ void TocModel::populate(Toc const & toc)
model_map_[iter] = top_level_item; model_map_[iter] = top_level_item;
LYXERR(Debug::GUI, "Toc: at depth " << iter->depth() LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
<< ", added item " << to_utf8(iter->str())); << ", added item " << toqstr(iter->str()));
populate(iter, end, top_level_item); populate(iter, end, top_level_item);
@ -110,10 +110,9 @@ void TocModel::populate(TocIterator & iter, TocIterator const & end,
int current_row; int current_row;
QModelIndex child_item; QModelIndex child_item;
insertColumns(0, 1, parent); insertColumns(0, 1, parent);
while (iter != end) {
while (iter != end) {
++iter; ++iter;
if (iter == end) if (iter == end)

View File

@ -54,12 +54,19 @@ using namespace lyx::support;
namespace lyx { namespace lyx {
LyXErr & operator<<(LyXErr & err, QString const & str)
{
return err << fromqstr(str);
}
FileName libFileSearch(QString const & dir, QString const & name, FileName libFileSearch(QString const & dir, QString const & name,
QString const & ext) QString const & ext)
{ {
return support::libFileSearch(fromqstr(dir), fromqstr(name), fromqstr(ext)); return support::libFileSearch(fromqstr(dir), fromqstr(name), fromqstr(ext));
} }
namespace frontend { namespace frontend {
string widgetsToLength(QLineEdit const * input, LengthCombo const * combo) string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)

View File

@ -15,9 +15,10 @@
#include "Length.h" #include "Length.h"
#include "support/qstring_helpers.h" #include "support/qstring_helpers.h"
#include "support/strfwd.h"
#include "qt_i18n.h" #include "qt_i18n.h"
#include <QString>
class QComboBox; class QComboBox;
class QLineEdit; class QLineEdit;
class QCheckBox; class QCheckBox;
@ -31,6 +32,10 @@ namespace lyx {
namespace support { class FileName; } namespace support { class FileName; }
class LyXErr;
LyXErr & operator<<(LyXErr &, QString const &);
namespace frontend { namespace frontend {
/// method to get a Length from widgets (LengthCombo) /// method to get a Length from widgets (LengthCombo)
@ -63,7 +68,7 @@ void setValid(QWidget * widget, bool valid);
QString const qt_(std::string const & str); QString const qt_(std::string const & str);
/// ///
typedef std::pair<QString, QString> LanguagePair; struct LanguagePair { QString first; QString second; };
/** If the caller is the character dialog, add "No change" and "Reset" /** If the caller is the character dialog, add "No change" and "Reset"
* to the vector. * to the vector.

View File

@ -5,6 +5,7 @@
* *
* \author Lars Gullik Bjønnes * \author Lars Gullik Bjønnes
* \author Jean-Marc Lasgouttes * \author Jean-Marc Lasgouttes
* \author Dekel Tsur
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
*/ */
@ -20,6 +21,9 @@
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include "support/assert.h" #include "support/assert.h"
#include <QString>
#include <QVector>
#include <algorithm> #include <algorithm>
using namespace std; using namespace std;
@ -56,6 +60,45 @@ static inline char_type qchar_to_ucs4(QChar const & qchar)
} }
QString toqstr(char const * str)
{
return QString::fromUtf8(str);
}
QString toqstr(std::string const & str)
{
return toqstr(str.c_str());
}
QString toqstr(docstring const & ucs4)
{
// If possible we let qt do the work, since this version does not
// need to be superfast.
return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
}
QString toqstr(char_type ucs4)
{
union { char_type c; uint i; } u = { ucs4 };
return QString::fromUcs4(&u.i, 1);
}
docstring qstring_to_ucs4(QString const & qstr)
{
if (qstr.isEmpty())
return docstring();
QVector<uint> const ucs4 = qstr.toUcs4();
return docstring((char_type const *)(ucs4.constData()), ucs4.size());
}
std::string fromqstr(QString const & str)
{
return str.isEmpty() ? std::string() : std::string(str.toUtf8());
}
/** /**
* Convert a UCS4 character into a QChar. * Convert a UCS4 character into a QChar.
* This is a hack (it does only make sense for the common part of the UCS4 * This is a hack (it does only make sense for the common part of the UCS4

View File

@ -0,0 +1,60 @@
/**
* \file qstring_helper.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Dekel Tsur
*
* Full author contact details are available in file CREDITS.
*
* A collection of unicode conversion functions, using iconv.
*/
#include <config.h>
#include "support/docstring.h"
#include "support/docstring.h"
#include <QString>
#include <QVector>
namespace lyx {
QString toqstr(char const * str)
{
return QString::fromUtf8(str);
}
QString toqstr(std::string const & str)
{
return toqstr(str.c_str());
}
QString toqstr(docstring const & ucs4)
{
// If possible we let qt do the work, since this version does not
// need to be superfast.
return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
}
QString toqstr(char_type ucs4)
{
union { char_type c; uint i; } u = { ucs4 };
return QString::fromUcs4(&u.i, 1);
}
docstring qstring_to_ucs4(QString const & qstr)
{
if (qstr.isEmpty())
return docstring();
QVector<uint> const ucs4 = qstr.toUcs4();
return docstring((char_type const *)(ucs4.constData()), ucs4.size());
}
std::string fromqstr(QString const & str)
{
return str.isEmpty() ? std::string() : std::string(str.toUtf8());
}
} // namespace lyx

View File

@ -12,10 +12,9 @@
#ifndef QSTRING_HELPERS_H #ifndef QSTRING_HELPERS_H
#define QSTRING_HELPERS_H #define QSTRING_HELPERS_H
#include "support/docstring.h" #include "support/strfwd.h"
#include <QString> class QString;
#include <QVector>
namespace lyx { namespace lyx {
@ -25,10 +24,7 @@ namespace lyx {
* This should not be used, since all possibly non-ASCII stuff should be * This should not be used, since all possibly non-ASCII stuff should be
* stored in a docstring. * stored in a docstring.
*/ */
inline QString const toqstr(char const * str) QString toqstr(char const * str);
{
return QString::fromUtf8(str);
}
/** /**
@ -37,14 +33,11 @@ inline QString const toqstr(char const * str)
* This should not be used, since all possibly non-ASCII stuff should be * This should not be used, since all possibly non-ASCII stuff should be
* stored in a docstring. * stored in a docstring.
*/ */
inline QString const toqstr(std::string const & str) QString toqstr(std::string const & str);
{
return toqstr(str.c_str());
}
/// Is \p c a valid utf16 char? /// Is \p c a valid utf16 char?
inline bool is_utf16(char_type c) inline bool is_utf16(unsigned int c)
{ {
// 0xd800 ... 0xdfff is the range of surrogate pairs. // 0xd800 ... 0xdfff is the range of surrogate pairs.
return c < 0xd800 || (c > 0xdfff && c < 0x10000); return c < 0xd800 || (c > 0xdfff && c < 0x10000);
@ -57,12 +50,7 @@ inline bool is_utf16(char_type c)
* This is the preferred method of converting anything that possibly * This is the preferred method of converting anything that possibly
* contains non-ASCII stuff to QString. * contains non-ASCII stuff to QString.
*/ */
inline QString const toqstr(docstring const & ucs4) QString toqstr(docstring const & ucs4);
{
// If possible we let qt do the work, since this version does not
// need to be superfast.
return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
}
/** /**
* toqstr - convert a UCS4 encoded character into a QString * toqstr - convert a UCS4 encoded character into a QString
@ -70,11 +58,7 @@ inline QString const toqstr(docstring const & ucs4)
* This is the preferred method of converting anything that possibly * This is the preferred method of converting anything that possibly
* contains non-ASCII stuff to QString. * contains non-ASCII stuff to QString.
*/ */
inline QString const toqstr(char_type ucs4) QString toqstr(char_type ucs4);
{
union { char_type c; uint i; } u = { ucs4 };
return QString::fromUcs4(&u.i, 1);
}
/** /**
* qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring * qstring_to_ucs4 - convert a QString into a UCS4 encoded docstring
@ -82,13 +66,7 @@ inline QString const toqstr(char_type ucs4)
* This is the preferred method of converting anything that possibly * This is the preferred method of converting anything that possibly
* contains non-ASCII stuff to docstring. * contains non-ASCII stuff to docstring.
*/ */
inline docstring const qstring_to_ucs4(QString const & qstr) docstring qstring_to_ucs4(QString const & qstr);
{
if (qstr.isEmpty())
return docstring();
QVector<uint> const ucs4 = qstr.toUcs4();
return docstring((char_type const *)(ucs4.constData()), ucs4.size());
}
/** /**
* fromqstr - convert a QString into a UTF8 encoded std::string * fromqstr - convert a QString into a UTF8 encoded std::string
@ -96,10 +74,7 @@ inline docstring const qstring_to_ucs4(QString const & qstr)
* This should not be used except for output to lyxerr, since all possibly * This should not be used except for output to lyxerr, since all possibly
* non-ASCII stuff should be stored in a docstring. * non-ASCII stuff should be stored in a docstring.
*/ */
inline std::string const fromqstr(QString const & str) std::string fromqstr(QString const & str);
{
return str.isEmpty() ? std::string() : std::string(str.toUtf8());
}
} // namespace lyx } // namespace lyx