lyx_mirror/src/frontends/qt4/qt_helpers.C
Georg Baum 77b9dbd557 Fix several filename and environment variable encoding problems
* src/LaTeX.C
	(LaTeX::deplog): Assume that filenames in log files are stored in
	the file system encoding

	* src/frontends/qt4/qt_helpers.[Ch]
	(internal_path): delete

	* src/frontends/qt4/QGraphics.C: Adjust to change above

	* src/frontends/qt4/QPrefsDialog.C: ditto

	* src/frontends/qt4/QExternal.C: ditto

	* src/frontends/qt4/QInclude.C: ditto

	* src/support/os.h: Document the encoding of filename arguments

	* src/support/os_win32.h: ditto

	* src/support/filetools.C
	(findtexfile): Convert filename from file system encoding

	* src/support/os_win32.C: Convert filenames from utf8 to file system
	encoding and vice versa where needed

	* src/support/os_cygwin.C: ditto

	* src/support/getcwd.C
	(getcwd): Use internal_path() with correct encoding

	* src/support/docstring.[Ch]
	(from_filesystem8bit): new conversion function

	* src/support/environment.C
	(getEnv): convert environment variable from local 8bit encoding to utf8
	(setEnv): convert environment variable from utf8 to local 8bit encoding

	* src/support/environment.h: document encoding of function arguments


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16753 a592a061-630c-0410-9148-cb99ea01b6c8
2007-01-18 20:47:27 +00:00

183 lines
3.8 KiB
C

/**
* \file qt_helpers.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Dekel Tsur
* \author Jürgen Spitzmüller
*
* 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 <qlineedit.h>
#include <qtextcodec.h>
#include <algorithm>
namespace lyx {
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,
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 {
combo->setCurrentItem(LyXLength(len).unit());
input->setText(toqstr(convert<string>(LyXLength(len).value())));
}
}
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 + lyx::char_type(' ') + word;
// FIXME: make w be size_t
if (int(line_plus_word.length()) >= w) {
sout += line + lyx::char_type('\n');
if (newline) {
sout += word + lyx::char_type('\n');
line.erase();
} else {
line = word;
}
} else if (newline) {
sout += line_plus_word + lyx::char_type('\n');
line.erase();
} else {
if (!line.empty())
line += lyx::char_type(' ');
line += word;
}
if (nxtpos == docstring::npos) {
if (!line.empty())
sout += line;
break;
}
curpos = nxtpos + 1;
}
return sout;
}
} // namespace lyx