2006-03-05 17:24:44 +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
|
|
|
|
|
* \author J<EFBFBD>rgen Spitzm<EFBFBD>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/lstrings.h"
|
|
|
|
|
#include "support/convert.h"
|
|
|
|
|
|
2006-08-30 14:59:07 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
#include <QComboBox>
|
|
|
|
|
#include <qlineedit.h>
|
|
|
|
|
#include <qtextcodec.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using lyx::support::isStrDbl;
|
2006-08-30 14:59:07 +00:00
|
|
|
|
using lyx::char_type;
|
2006-09-03 07:02:38 +00:00
|
|
|
|
using lyx::docstring;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2006-08-30 14:59:07 +00:00
|
|
|
|
using std::vector;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
using std::make_pair;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::pair;
|
2006-08-30 14:59:07 +00:00
|
|
|
|
using std::endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string makeFontName(string const & family, string const & foundry)
|
|
|
|
|
{
|
|
|
|
|
if (foundry.empty())
|
|
|
|
|
return family;
|
|
|
|
|
return family + " [" + foundry + ']';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
|
pair<string, string> parseFontName(string const & name)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
QString const length = input->text();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
if (length.isEmpty())
|
|
|
|
|
return string();
|
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
|
// Don't return unit-from-choice if the input(field) contains a unit
|
2006-03-05 17:24:44 +00:00
|
|
|
|
if (isValidGlueLength(fromqstr(length)))
|
|
|
|
|
return fromqstr(length);
|
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
|
LyXLength::UNIT const unit = combo->currentLengthItem();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
return LyXLength(length.toDouble(), unit).asString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLength widgetsToLength(QLineEdit const * input, QComboBox const * combo)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
QString const length = input->text();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
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));
|
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
|
LyXLength::UNIT const unit = unitFromString(fromqstr(combo->currentText()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
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 toqstr(char const * str)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
return QString::fromUtf8(str);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const toqstr(string const & str)
|
|
|
|
|
{
|
|
|
|
|
return toqstr(str.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-07 16:30:26 +00:00
|
|
|
|
void ucs4_to_qstring(char_type const * str, size_t ls, QString & s)
|
2006-08-30 14:59:07 +00:00
|
|
|
|
{
|
2006-10-07 16:15:06 +00:00
|
|
|
|
s.reserve(ls);
|
2006-10-09 12:07:05 +00:00
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ls; ++i)
|
|
|
|
|
s.append(ucs4_to_qchar(str[i]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ucs4_to_qstring(lyx::docstring const & str, QString & s)
|
|
|
|
|
{
|
|
|
|
|
size_t ls = str.size();
|
|
|
|
|
s.reserve(ls);
|
|
|
|
|
s.clear();
|
2006-08-30 14:59:07 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ls; ++i)
|
|
|
|
|
s.append(ucs4_to_qchar(str[i]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
|
QString const toqstr(docstring const & ucs4)
|
2006-08-30 14:59:07 +00:00
|
|
|
|
{
|
|
|
|
|
QString s;
|
2006-10-09 12:30:55 +00:00
|
|
|
|
ucs4_to_qstring(ucs4, s);
|
2006-08-30 14:59:07 +00:00
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
|
docstring const qstring_to_ucs4(QString const & qstr)
|
2006-08-30 14:59:07 +00:00
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
int const ls = qstr.size();
|
2006-09-03 07:02:38 +00:00
|
|
|
|
docstring ucs4;
|
2006-08-31 20:36:07 +00:00
|
|
|
|
for (int i = 0; i < ls; ++i)
|
2006-09-03 07:02:38 +00:00
|
|
|
|
ucs4 += static_cast<char_type>(qstr[i].unicode());
|
2006-08-30 14:59:07 +00:00
|
|
|
|
|
|
|
|
|
return ucs4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void qstring_to_ucs4(QString const & qstr, vector<char_type> & ucs4)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
int const ls = qstr.size();
|
2006-08-30 14:59:07 +00:00
|
|
|
|
ucs4.clear();
|
2006-08-31 20:36:07 +00:00
|
|
|
|
for (int i = 0; i < ls; ++i)
|
2006-09-13 17:11:39 +00:00
|
|
|
|
ucs4.push_back(static_cast<lyx::char_type>(qstr[i].unicode()));
|
2006-08-30 14:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
QString const qt_(char const * str)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
return toqstr(_(str));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString const qt_(string const & str)
|
|
|
|
|
{
|
2006-09-09 22:27:22 +00:00
|
|
|
|
return toqstr(_(str));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const fromqstr(QString const & str)
|
|
|
|
|
{
|
2006-08-17 09:00:36 +00:00
|
|
|
|
return str.isEmpty()? string(): string(str.toAscii());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const formatted(docstring const & text, int w)
|
2006-03-05 17:24:44 +00:00
|
|
|
|
{
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring sout;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
if (text.empty())
|
|
|
|
|
return sout;
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring::size_type curpos = 0;
|
|
|
|
|
docstring line;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
for (;;) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
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);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const word =
|
|
|
|
|
nxtpos == docstring::npos ?
|
2006-09-09 22:27:22 +00:00
|
|
|
|
text.substr(curpos) :
|
|
|
|
|
text.substr(curpos, nxtpos - curpos);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
bool const newline = (nxtpos2 != docstring::npos &&
|
2006-03-05 17:24:44 +00:00
|
|
|
|
nxtpos2 < nxtpos1);
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const line_plus_word =
|
|
|
|
|
line.empty() ? word : line + lyx::char_type(' ') + word;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
// FIXME: make w be size_t
|
|
|
|
|
if (int(line_plus_word.length()) >= w) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
sout += line + lyx::char_type('\n');
|
2006-03-05 17:24:44 +00:00
|
|
|
|
if (newline) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
sout += word + lyx::char_type('\n');
|
2006-03-05 17:24:44 +00:00
|
|
|
|
line.erase();
|
|
|
|
|
} else {
|
|
|
|
|
line = word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (newline) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
sout += line_plus_word + lyx::char_type('\n');
|
2006-03-05 17:24:44 +00:00
|
|
|
|
line.erase();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if (!line.empty())
|
2006-09-11 08:54:10 +00:00
|
|
|
|
line += lyx::char_type(' ');
|
2006-03-05 17:24:44 +00:00
|
|
|
|
line += word;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
if (nxtpos == docstring::npos) {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
if (!line.empty())
|
|
|
|
|
sout += line;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curpos = nxtpos + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sout;
|
|
|
|
|
}
|