Windows:confgui: process windows file names as unicode, no local8bit

This commit is contained in:
Jean-Francois Dockes 2019-07-22 15:48:40 +02:00
parent 30f37f0e23
commit 21b918d447

View File

@ -56,9 +56,12 @@ static const int margin = 2;
void ConfParamW::setValue(const QString& value) void ConfParamW::setValue(const QString& value)
{ {
#ifndef _WIN32
// On Windows all paths are unicode.
if (m_fsencoding) if (m_fsencoding)
m_cflink->set(string((const char *)value.toLocal8Bit())); m_cflink->set(string((const char *)value.toLocal8Bit()));
else else
#endif
m_cflink->set(string((const char *)value.toUtf8())); m_cflink->set(string((const char *)value.toUtf8()));
} }
@ -76,8 +79,8 @@ void ConfParamW::setValue(bool value)
} }
void setSzPol(QWidget *w, QSizePolicy::Policy hpol, void setSzPol(QWidget *w, QSizePolicy::Policy hpol,
QSizePolicy::Policy vpol, QSizePolicy::Policy vpol,
int hstretch, int vstretch) int hstretch, int vstretch)
{ {
QSizePolicy policy(hpol, vpol); QSizePolicy policy(hpol, vpol);
policy.setHorizontalStretch(hstretch); policy.setHorizontalStretch(hstretch);
@ -102,15 +105,15 @@ bool ConfParamW::createCommon(const QString& lbltxt, const QString& tltptxt)
} }
ConfParamIntW::ConfParamIntW(QWidget *parent, ConfLink cflink, ConfParamIntW::ConfParamIntW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt, const QString& tltptxt,
int minvalue, int minvalue,
int maxvalue, int maxvalue,
int defaultvalue) int defaultvalue)
: ConfParamW(parent, cflink), m_defaultvalue(defaultvalue) : ConfParamW(parent, cflink), m_defaultvalue(defaultvalue)
{ {
if (!createCommon(lbltxt, tltptxt)) if (!createCommon(lbltxt, tltptxt))
return; return;
m_sb = new QSpinBox(this); m_sb = new QSpinBox(this);
m_sb->setMinimum(minvalue); m_sb->setMinimum(minvalue);
@ -124,7 +127,7 @@ ConfParamIntW::ConfParamIntW(QWidget *parent, ConfLink cflink,
loadValue(); loadValue();
QObject::connect(m_sb, SIGNAL(valueChanged(int)), QObject::connect(m_sb, SIGNAL(valueChanged(int)),
this, SLOT(setValue(int))); this, SLOT(setValue(int)));
} }
void ConfParamIntW::loadValue() void ConfParamIntW::loadValue()
@ -137,12 +140,12 @@ void ConfParamIntW::loadValue()
} }
ConfParamStrW::ConfParamStrW(QWidget *parent, ConfLink cflink, ConfParamStrW::ConfParamStrW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt) const QString& tltptxt)
: ConfParamW(parent, cflink) : ConfParamW(parent, cflink)
{ {
if (!createCommon(lbltxt, tltptxt)) if (!createCommon(lbltxt, tltptxt))
return; return;
m_le = new QLineEdit(this); m_le = new QLineEdit(this);
setSzPol(m_le, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0); setSzPol(m_le, QSizePolicy::Preferred, QSizePolicy::Fixed, 1, 0);
@ -151,28 +154,31 @@ ConfParamStrW::ConfParamStrW(QWidget *parent, ConfLink cflink,
loadValue(); loadValue();
QObject::connect(m_le, SIGNAL(textChanged(const QString&)), QObject::connect(m_le, SIGNAL(textChanged(const QString&)),
this, SLOT(setValue(const QString&))); this, SLOT(setValue(const QString&)));
} }
void ConfParamStrW::loadValue() void ConfParamStrW::loadValue()
{ {
string s; string s;
m_cflink->get(s); m_cflink->get(s);
#ifndef _WIN32
// On Windows all paths are unicode.
if (m_fsencoding) if (m_fsencoding)
m_le->setText(QString::fromLocal8Bit(s.c_str())); m_le->setText(QString::fromLocal8Bit(s.c_str()));
else else
#endif
m_le->setText(QString::fromUtf8(s.c_str())); m_le->setText(QString::fromUtf8(s.c_str()));
} }
ConfParamCStrW::ConfParamCStrW(QWidget *parent, ConfLink cflink, ConfParamCStrW::ConfParamCStrW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt, const QString& tltptxt,
const QStringList &sl const QStringList &sl
) )
: ConfParamW(parent, cflink) : ConfParamW(parent, cflink)
{ {
if (!createCommon(lbltxt, tltptxt)) if (!createCommon(lbltxt, tltptxt))
return; return;
m_cmb = new QComboBox(this); m_cmb = new QComboBox(this);
m_cmb->setEditable(false); m_cmb->setEditable(false);
m_cmb->insertItems(0, sl); m_cmb->insertItems(0, sl);
@ -183,7 +189,7 @@ ConfParamCStrW::ConfParamCStrW(QWidget *parent, ConfLink cflink,
loadValue(); loadValue();
QObject::connect(m_cmb, SIGNAL(activated(const QString&)), QObject::connect(m_cmb, SIGNAL(activated(const QString&)),
this, SLOT(setValue(const QString&))); this, SLOT(setValue(const QString&)));
} }
void ConfParamCStrW::loadValue() void ConfParamCStrW::loadValue()
@ -191,22 +197,25 @@ void ConfParamCStrW::loadValue()
string s; string s;
m_cflink->get(s); m_cflink->get(s);
QString cs; QString cs;
#ifndef _WIN32
// On Windows all paths are unicode.
if (m_fsencoding) if (m_fsencoding)
cs = QString::fromLocal8Bit(s.c_str()); cs = QString::fromLocal8Bit(s.c_str());
else else
#endif
cs = QString::fromUtf8(s.c_str()); cs = QString::fromUtf8(s.c_str());
for (int i = 0; i < m_cmb->count(); i++) { for (int i = 0; i < m_cmb->count(); i++) {
if (!cs.compare(m_cmb->itemText(i))) { if (!cs.compare(m_cmb->itemText(i))) {
m_cmb->setCurrentIndex(i); m_cmb->setCurrentIndex(i);
break; break;
} }
} }
} }
ConfParamBoolW::ConfParamBoolW(QWidget *parent, ConfLink cflink, ConfParamBoolW::ConfParamBoolW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt) const QString& tltptxt)
: ConfParamW(parent, cflink) : ConfParamW(parent, cflink)
{ {
// No createCommon because the checkbox has a label // No createCommon because the checkbox has a label
@ -234,17 +243,17 @@ void ConfParamBoolW::loadValue()
} }
ConfParamFNW::ConfParamFNW(QWidget *parent, ConfLink cflink, ConfParamFNW::ConfParamFNW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt, const QString& tltptxt,
bool isdir, bool isdir,
QString dirloc, QString dirloc,
QString dfltnm QString dfltnm
) )
: ConfParamW(parent, cflink), m_isdir(isdir), m_dirloc(dirloc), : ConfParamW(parent, cflink), m_isdir(isdir), m_dirloc(dirloc),
m_dfltnm(dfltnm) m_dfltnm(dfltnm)
{ {
if (!createCommon(lbltxt, tltptxt)) if (!createCommon(lbltxt, tltptxt))
return; return;
m_fsencoding = true; m_fsencoding = true;
@ -265,14 +274,19 @@ ConfParamFNW::ConfParamFNW(QWidget *parent, ConfLink cflink,
loadValue(); loadValue();
QObject::connect(m_pb, SIGNAL(clicked()), this, SLOT(showBrowserDialog())); QObject::connect(m_pb, SIGNAL(clicked()), this, SLOT(showBrowserDialog()));
QObject::connect(m_le, SIGNAL(textChanged(const QString&)), QObject::connect(m_le, SIGNAL(textChanged(const QString&)),
this, SLOT(setValue(const QString&))); this, SLOT(setValue(const QString&)));
} }
void ConfParamFNW::loadValue() void ConfParamFNW::loadValue()
{ {
string s; string s;
m_cflink->get(s); m_cflink->get(s);
#ifndef _WIN32
// On Windows all paths are unicode.
m_le->setText(QString::fromLocal8Bit(s.c_str())); m_le->setText(QString::fromLocal8Bit(s.c_str()));
#else
m_le->setText(QString::fromUtf8(s.c_str()));
#endif
} }
void ConfParamFNW::showBrowserDialog() void ConfParamFNW::showBrowserDialog()
@ -286,13 +300,13 @@ class SmallerListWidget: public QListWidget
{ {
public: public:
SmallerListWidget(QWidget *parent) SmallerListWidget(QWidget *parent)
: QListWidget(parent) {} : QListWidget(parent) {}
virtual QSize sizeHint() const {return QSize(150, 40);} virtual QSize sizeHint() const {return QSize(150, 40);}
}; };
ConfParamSLW::ConfParamSLW(QWidget *parent, ConfLink cflink, ConfParamSLW::ConfParamSLW(QWidget *parent, ConfLink cflink,
const QString& lbltxt, const QString& lbltxt,
const QString& tltptxt) const QString& tltptxt)
: ConfParamW(parent, cflink) : ConfParamW(parent, cflink)
{ {
// Can't use createCommon here cause we want the buttons below the label // Can't use createCommon here cause we want the buttons below the label
@ -346,9 +360,12 @@ void ConfParamSLW::loadValue()
stringToStrings(s, ls); stringToStrings(s, ls);
QStringList qls; QStringList qls;
for (list<string>::const_iterator it = ls.begin(); it != ls.end(); it++) { for (list<string>::const_iterator it = ls.begin(); it != ls.end(); it++) {
// On Windows all paths are unicode.
#ifndef _WIN32
if (m_fsencoding) if (m_fsencoding)
qls.push_back(QString::fromLocal8Bit(it->c_str())); qls.push_back(QString::fromLocal8Bit(it->c_str()));
else else
#endif
qls.push_back(QString::fromUtf8(it->c_str())); qls.push_back(QString::fromUtf8(it->c_str()));
} }
m_lb->clear(); m_lb->clear();
@ -359,27 +376,27 @@ void ConfParamSLW::showInputDialog()
{ {
bool ok; bool ok;
QString s = QInputDialog::getText (this, QString s = QInputDialog::getText (this,
"", // title "", // title
"", // label, "", // label,
QLineEdit::Normal, // EchoMode mode QLineEdit::Normal, // EchoMode mode
"", // const QString & text "", // const QString & text
&ok); &ok);
if (ok && !s.isEmpty()) { if (ok && !s.isEmpty()) {
QList<QListWidgetItem *>items = QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive); m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) { if (items.empty()) {
m_lb->insertItem(0, s); m_lb->insertItem(0, s);
m_lb->sortItems(); m_lb->sortItems();
listToConf(); listToConf();
} }
} }
} }
void ConfParamSLW::listToConf() void ConfParamSLW::listToConf()
{ {
list<string> ls; list<string> ls;
LOGDEB2("ConfParamSLW::listToConf. m_fsencoding " << (int(m_fsencoding)) << "\n" ); LOGDEB2("ConfParamSLW::listToConf. m_fsencoding " << m_fsencoding << "\n");
for (int i = 0; i < m_lb->count(); i++) { for (int i = 0; i < m_lb->count(); i++) {
// General parameters are encoded as utf-8. File names as // General parameters are encoded as utf-8. File names as
// local8bit There is no hope for 8bit file names anyway // local8bit There is no hope for 8bit file names anyway
@ -388,10 +405,11 @@ void ConfParamSLW::listToConf()
// backslashes to slashes. This is an awful hack because // backslashes to slashes. This is an awful hack because
// fsencoding does not necessarily imply that the values are // fsencoding does not necessarily imply that the values are
// paths, and it will come back to haunt us one day. // paths, and it will come back to haunt us one day.
QString text = m_lb->item(i)->text(); QString text = m_lb->item(i)->text();
if (m_fsencoding) { if (m_fsencoding) {
// On Windows all paths are unicode.
#ifdef _WIN32 #ifdef _WIN32
string pth((const char *)(text.toLocal8Bit())); string pth((const char *)(text.toUtf8()));
path_slashize(pth); path_slashize(pth);
ls.push_back(pth); ls.push_back(pth);
#else #else
@ -420,16 +438,16 @@ void ConfParamSLW::deleteSelected()
vector<int> idxes; vector<int> idxes;
for (int i = 0; i < m_lb->count(); i++) { for (int i = 0; i < m_lb->count(); i++) {
if (m_lb->item(i)->isSelected()) { if (m_lb->item(i)->isSelected()) {
idxes.push_back(i); idxes.push_back(i);
} }
} }
for (vector<int>::reverse_iterator it = idxes.rbegin(); for (vector<int>::reverse_iterator it = idxes.rbegin();
it != idxes.rend(); it++) { it != idxes.rend(); it++) {
LOGDEB0("deleteSelected: " << (*it) << " was selected\n" ); LOGDEB0("deleteSelected: " << *it << " was selected\n");
QListWidgetItem *item = m_lb->takeItem(*it); QListWidgetItem *item = m_lb->takeItem(*it);
emit entryDeleted(item->text()); emit entryDeleted(item->text());
delete item; delete item;
} }
listToConf(); listToConf();
@ -440,18 +458,18 @@ void ConfParamDNLW::showInputDialog()
{ {
QString s = myGetFileName(true); QString s = myGetFileName(true);
if (!s.isEmpty()) { if (!s.isEmpty()) {
QList<QListWidgetItem *>items = QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive); m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) { if (items.empty()) {
m_lb->insertItem(0, s); m_lb->insertItem(0, s);
m_lb->sortItems(); m_lb->sortItems();
QList<QListWidgetItem *>items = QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive); m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (m_lb->selectionMode() == QAbstractItemView::SingleSelection && if (m_lb->selectionMode() == QAbstractItemView::SingleSelection &&
!items.empty()) !items.empty())
m_lb->setCurrentItem(*items.begin()); m_lb->setCurrentItem(*items.begin());
listToConf(); listToConf();
} }
} }
} }
@ -460,23 +478,22 @@ void ConfParamCSLW::showInputDialog()
{ {
bool ok; bool ok;
QString s = QInputDialog::getItem (this, // parent QString s = QInputDialog::getItem (this, // parent
"", // title "", // title
"", // label, "", // label,
m_sl, // items, m_sl, // items,
0, // current = 0 0, // current = 0
false, // editable = true, false, // editable = true,
&ok); &ok);
if (ok && !s.isEmpty()) { if (ok && !s.isEmpty()) {
QList<QListWidgetItem *>items = QList<QListWidgetItem *>items =
m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive); m_lb->findItems(s, Qt::MatchFixedString|Qt::MatchCaseSensitive);
if (items.empty()) { if (items.empty()) {
m_lb->insertItem(0, s); m_lb->insertItem(0, s);
m_lb->sortItems(); m_lb->sortItems();
listToConf(); listToConf();
} }
} }
} }
} // Namespace confgui } // Namespace confgui