2002-11-27 21:49:50 +00:00
|
|
|
|
/**
|
|
|
|
|
* \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
|
2003-11-29 17:25:31 +00:00
|
|
|
|
* \author J<EFBFBD>rgen Spitzm<EFBFBD>ller
|
2002-11-27 21:49:50 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-11-27 21:49:50 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
|
#include "lengthcombo.h"
|
2002-11-27 21:49:50 +00:00
|
|
|
|
#include "qt_helpers.h"
|
2003-02-25 13:35:26 +00:00
|
|
|
|
|
2004-11-27 18:47:41 +00:00
|
|
|
|
#include "lengthcommon.h"
|
2004-05-20 09:36:28 +00:00
|
|
|
|
#include "gettext.h"
|
|
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
2005-01-06 16:44:04 +00:00
|
|
|
|
#include "support/convert.h"
|
2003-02-25 13:35:26 +00:00
|
|
|
|
|
2004-11-27 18:47:41 +00:00
|
|
|
|
#include <qcombobox.h>
|
2002-12-04 02:57:14 +00:00
|
|
|
|
#include <qlineedit.h>
|
2002-12-17 20:37:13 +00:00
|
|
|
|
#include <qtextcodec.h>
|
2002-11-27 21:49:50 +00:00
|
|
|
|
|
2003-09-08 16:23:31 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2003-11-29 17:25:31 +00:00
|
|
|
|
|
|
|
|
|
using lyx::support::isStrDbl;
|
|
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::make_pair;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2002-11-28 19:31:59 +00:00
|
|
|
|
using std::pair;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2002-11-27 21:49:50 +00:00
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2002-12-04 02:57:14 +00:00
|
|
|
|
|
2003-02-25 13:35:26 +00:00
|
|
|
|
|
2002-12-04 02:57:14 +00:00
|
|
|
|
string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
|
|
|
|
|
{
|
|
|
|
|
QString length = input->text();
|
|
|
|
|
if (length.isEmpty())
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
// don't return unit-from-choice if the input(field) contains a unit
|
2002-12-17 20:37:13 +00:00
|
|
|
|
if (isValidGlueLength(fromqstr(length)))
|
|
|
|
|
return fromqstr(length);
|
2002-12-04 02:57:14 +00:00
|
|
|
|
|
|
|
|
|
LyXLength::UNIT unit = combo->currentLengthItem();
|
|
|
|
|
|
|
|
|
|
return LyXLength(length.toDouble(), unit).asString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-11-27 18:47:41 +00:00
|
|
|
|
LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
|
|
|
|
|
{
|
|
|
|
|
QString 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 unit = unitFromString(fromqstr(combo->currentText()));
|
2005-01-06 15:40:49 +00:00
|
|
|
|
|
2004-11-27 18:47:41 +00:00
|
|
|
|
return LyXLength(length.toDouble(), unit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-12-04 02:57:14 +00:00
|
|
|
|
void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
|
|
|
|
|
string const & len, LyXLength::UNIT defaultUnit)
|
|
|
|
|
{
|
|
|
|
|
if (len.empty()) {
|
|
|
|
|
// no length (UNIT_NONE)
|
|
|
|
|
combo->setCurrentItem(defaultUnit);
|
|
|
|
|
input->setText("");
|
2003-11-29 17:25:31 +00:00
|
|
|
|
} else if (!isValidLength(len) && !isStrDbl(len)) {
|
|
|
|
|
// use input field only for gluelengths
|
|
|
|
|
combo->setCurrentItem(defaultUnit);
|
|
|
|
|
input->setText(toqstr(len));
|
2002-12-04 02:57:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
combo->setCurrentItem(LyXLength(len).unit());
|
2005-01-06 15:40:49 +00:00
|
|
|
|
input->setText(toqstr(convert<string>(LyXLength(len).value())));
|
2002-12-04 02:57:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-12-17 20:37:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const toqstr(char const * str)
|
|
|
|
|
{
|
2005-07-16 15:55:36 +00:00
|
|
|
|
return QString::fromAscii(str);
|
2002-12-17 20:37:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const toqstr(string const & str)
|
|
|
|
|
{
|
|
|
|
|
return toqstr(str.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const qt_(char const * str)
|
|
|
|
|
{
|
|
|
|
|
return toqstr(_(str));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const qt_(string const & str)
|
|
|
|
|
{
|
|
|
|
|
return toqstr(_(str));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const fromqstr(QString const & str)
|
|
|
|
|
{
|
2005-07-16 15:55:36 +00:00
|
|
|
|
//return str;
|
|
|
|
|
|
|
|
|
|
return str.ascii() ? str.ascii() : "";
|
2002-12-17 20:37:13 +00:00
|
|
|
|
}
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const formatted(string const & text, int w)
|
|
|
|
|
{
|
|
|
|
|
string sout;
|
|
|
|
|
|
|
|
|
|
if (text.empty())
|
|
|
|
|
return sout;
|
|
|
|
|
|
|
|
|
|
string::size_type curpos = 0;
|
|
|
|
|
string line;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
string::size_type const nxtpos1 = text.find(' ', curpos);
|
|
|
|
|
string::size_type const nxtpos2 = text.find('\n', curpos);
|
|
|
|
|
string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
|
|
|
|
|
|
|
|
|
|
string const word = nxtpos == string::npos ?
|
|
|
|
|
text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
|
|
|
|
|
|
|
|
|
|
bool const newline = (nxtpos2 != string::npos &&
|
|
|
|
|
nxtpos2 < nxtpos1);
|
|
|
|
|
|
|
|
|
|
string const line_plus_word =
|
|
|
|
|
line.empty() ? word : line + ' ' + word;
|
|
|
|
|
|
|
|
|
|
// FIXME: make w be size_t
|
2003-11-20 01:22:51 +00:00
|
|
|
|
if (int(line_plus_word.length()) >= w) {
|
2003-03-29 07:09:13 +00:00
|
|
|
|
sout += line + '\n';
|
|
|
|
|
if (newline) {
|
|
|
|
|
sout += word + '\n';
|
|
|
|
|
line.erase();
|
|
|
|
|
} else {
|
|
|
|
|
line = word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (newline) {
|
|
|
|
|
sout += line_plus_word + '\n';
|
|
|
|
|
line.erase();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if (!line.empty())
|
|
|
|
|
line += ' ';
|
|
|
|
|
line += word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nxtpos == string::npos) {
|
|
|
|
|
if (!line.empty())
|
|
|
|
|
sout += line;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curpos = nxtpos + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sout;
|
|
|
|
|
}
|