2009-04-16 07:29:01 +00:00
|
|
|
/**
|
|
|
|
* \file GuiIndices.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Edwin Leuven
|
|
|
|
* \author Jürgen Spitzmüller
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "GuiIndices.h"
|
|
|
|
|
|
|
|
#include "ColorCache.h"
|
|
|
|
#include "GuiApplication.h"
|
|
|
|
#include "Validator.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
|
|
|
#include "BufferParams.h"
|
2009-05-22 07:20:00 +00:00
|
|
|
#include "LyXRC.h"
|
2009-04-16 07:29:01 +00:00
|
|
|
|
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QTreeWidgetItem>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QColorDialog>
|
|
|
|
|
|
|
|
|
2009-05-22 07:20:00 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
|
|
|
2009-04-16 07:29:01 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
|
|
|
|
GuiIndices::GuiIndices(QWidget * parent)
|
2015-06-03 14:54:54 +00:00
|
|
|
: QWidget(parent), readonly_(false),
|
|
|
|
use_indices_(false)
|
2009-04-16 07:29:01 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
indicesTW->setColumnCount(2);
|
|
|
|
indicesTW->headerItem()->setText(0, qt_("Name"));
|
|
|
|
indicesTW->headerItem()->setText(1, qt_("Label Color"));
|
|
|
|
indicesTW->setSortingEnabled(true);
|
2009-05-22 07:20:00 +00:00
|
|
|
|
2011-02-04 07:37:12 +00:00
|
|
|
// NOTE: we do not provide "custom" here for security reasons!
|
2009-05-22 07:20:00 +00:00
|
|
|
indexCO->clear();
|
|
|
|
indexCO->addItem(qt_("Default"), QString("default"));
|
2009-05-28 12:49:41 +00:00
|
|
|
for (set<string>::const_iterator it = lyxrc.index_alternatives.begin();
|
2009-05-22 07:20:00 +00:00
|
|
|
it != lyxrc.index_alternatives.end(); ++it) {
|
|
|
|
QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
|
|
|
|
indexCO->addItem(command, command);
|
|
|
|
}
|
2011-09-24 16:39:23 +00:00
|
|
|
|
|
|
|
indexOptionsLE->setValidator(new NoNewLineValidator(indexOptionsLE));
|
|
|
|
newIndexLE->setValidator(new NoNewLineValidator(newIndexLE));
|
2009-04-16 07:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 14:54:54 +00:00
|
|
|
|
|
|
|
void GuiIndices::updateWidgets()
|
|
|
|
{
|
|
|
|
bool const have_sel =
|
|
|
|
!indicesTW->selectedItems().isEmpty();
|
|
|
|
|
|
|
|
indexCO->setEnabled(!readonly_);
|
|
|
|
indexOptionsLE->setEnabled(
|
|
|
|
indexCO->itemData(indexCO->currentIndex()).toString() != "default");
|
|
|
|
indexOptionsLE->setReadOnly(readonly_);
|
|
|
|
|
|
|
|
multipleIndicesCB->setEnabled(!readonly_);
|
|
|
|
indicesTW->setEnabled(use_indices_);
|
|
|
|
newIndexLE->setEnabled(use_indices_);
|
|
|
|
newIndexLE->setReadOnly(readonly_);
|
|
|
|
newIndexLA->setEnabled(use_indices_);
|
|
|
|
addIndexPB->setEnabled(use_indices_ && !readonly_
|
|
|
|
&& !newIndexLE->text().isEmpty());
|
|
|
|
availableLA->setEnabled(use_indices_);
|
|
|
|
removePB->setEnabled(use_indices_ && have_sel && !readonly_);
|
|
|
|
colorPB->setEnabled(use_indices_ && have_sel && !readonly_);
|
|
|
|
renamePB->setEnabled(use_indices_ && have_sel && !readonly_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::update(BufferParams const & params, bool const readonly)
|
2009-04-16 07:29:01 +00:00
|
|
|
{
|
2015-06-03 14:54:54 +00:00
|
|
|
use_indices_ = params.use_indices;
|
|
|
|
readonly_ = readonly;
|
2009-04-16 07:29:01 +00:00
|
|
|
indiceslist_ = params.indiceslist();
|
2009-04-22 21:17:49 +00:00
|
|
|
multipleIndicesCB->setChecked(params.use_indices);
|
2015-06-03 14:54:54 +00:00
|
|
|
|
|
|
|
updateWidgets();
|
2009-05-22 07:20:00 +00:00
|
|
|
|
|
|
|
string command;
|
|
|
|
string options =
|
|
|
|
split(params.index_command, command, ' ');
|
|
|
|
|
|
|
|
int const pos = indexCO->findData(toqstr(command));
|
|
|
|
if (pos != -1) {
|
|
|
|
indexCO->setCurrentIndex(pos);
|
2011-09-24 16:39:20 +00:00
|
|
|
indexOptionsLE->setText(toqstr(options).trimmed());
|
2009-05-22 07:20:00 +00:00
|
|
|
} else {
|
2011-02-04 07:37:12 +00:00
|
|
|
// We reset to default if we do not know the specified compiler
|
|
|
|
// This is for security reasons
|
|
|
|
indexCO->setCurrentIndex(indexCO->findData(toqstr("default")));
|
2011-09-24 16:39:20 +00:00
|
|
|
indexOptionsLE->clear();
|
2009-05-22 07:20:00 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 07:29:01 +00:00
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::updateView()
|
|
|
|
{
|
|
|
|
// store the selected index
|
|
|
|
QTreeWidgetItem * item = indicesTW->currentItem();
|
|
|
|
QString sel_index;
|
|
|
|
if (item != 0)
|
|
|
|
sel_index = item->text(0);
|
|
|
|
|
|
|
|
indicesTW->clear();
|
|
|
|
|
|
|
|
IndicesList::const_iterator it = indiceslist_.begin();
|
|
|
|
IndicesList::const_iterator const end = indiceslist_.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
|
|
|
|
|
|
|
|
QString const iname = toqstr(it->index());
|
|
|
|
newItem->setText(0, iname);
|
|
|
|
|
|
|
|
QColor const itemcolor = rgb2qcolor(it->color());
|
|
|
|
if (itemcolor.isValid()) {
|
|
|
|
QPixmap coloritem(30, 10);
|
|
|
|
coloritem.fill(itemcolor);
|
|
|
|
newItem->setIcon(1, QIcon(coloritem));
|
|
|
|
}
|
|
|
|
// restore selected index
|
|
|
|
if (iname == sel_index) {
|
|
|
|
indicesTW->setCurrentItem(newItem);
|
|
|
|
indicesTW->setItemSelected(newItem, true);
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 07:49:28 +00:00
|
|
|
indicesTW->resizeColumnToContents(0);
|
2015-06-03 14:54:54 +00:00
|
|
|
|
|
|
|
updateWidgets();
|
2009-04-16 07:29:01 +00:00
|
|
|
// emit signal
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::apply(BufferParams & params) const
|
|
|
|
{
|
|
|
|
params.use_indices = multipleIndicesCB->isChecked();
|
|
|
|
params.indiceslist() = indiceslist_;
|
2009-05-22 07:20:00 +00:00
|
|
|
|
|
|
|
string const index_command =
|
|
|
|
fromqstr(indexCO->itemData(
|
|
|
|
indexCO->currentIndex()).toString());
|
2011-09-24 16:39:20 +00:00
|
|
|
string const index_options = fromqstr(indexOptionsLE->text());
|
2011-02-04 07:37:12 +00:00
|
|
|
if (index_command == "default" || index_options.empty())
|
2009-05-22 07:20:00 +00:00
|
|
|
params.index_command = index_command;
|
|
|
|
else
|
2009-05-28 00:25:40 +00:00
|
|
|
params.index_command = index_command + " " + index_options;
|
2009-05-22 07:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-03 14:54:54 +00:00
|
|
|
void GuiIndices::on_indexCO_activated(int)
|
2009-05-22 07:20:00 +00:00
|
|
|
{
|
2015-06-03 14:54:54 +00:00
|
|
|
updateWidgets();
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::on_newIndexLE_textChanged(QString)
|
|
|
|
{
|
|
|
|
updateWidgets();
|
2009-05-22 07:20:00 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-24 16:39:20 +00:00
|
|
|
void GuiIndices::on_indexOptionsLE_textChanged(QString)
|
2009-05-22 07:20:00 +00:00
|
|
|
{
|
|
|
|
changed();
|
2009-04-16 07:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::on_addIndexPB_pressed()
|
|
|
|
{
|
|
|
|
QString const new_index = newIndexLE->text();
|
|
|
|
if (!new_index.isEmpty()) {
|
|
|
|
indiceslist_.add(qstring_to_ucs4(new_index));
|
|
|
|
newIndexLE->clear();
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::on_removePB_pressed()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem * selItem = indicesTW->currentItem();
|
|
|
|
QString sel_index;
|
|
|
|
if (selItem != 0)
|
|
|
|
sel_index = selItem->text(0);
|
|
|
|
if (!sel_index.isEmpty()) {
|
2017-07-03 17:53:14 +00:00
|
|
|
if (indiceslist_.find(qstring_to_ucs4(sel_index)) ==
|
2009-04-16 07:29:01 +00:00
|
|
|
indiceslist_.findShortcut(from_ascii("idx"))) {
|
2017-07-03 17:53:14 +00:00
|
|
|
Alert::error(_("Cannot remove standard index"),
|
2009-04-16 07:29:01 +00:00
|
|
|
_("The default index cannot be removed."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
indiceslist_.remove(qstring_to_ucs4(sel_index));
|
|
|
|
newIndexLE->clear();
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::on_renamePB_clicked()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem * selItem = indicesTW->currentItem();
|
|
|
|
QString sel_index;
|
|
|
|
if (selItem != 0)
|
|
|
|
sel_index = selItem->text(0);
|
|
|
|
if (!sel_index.isEmpty()) {
|
|
|
|
docstring newname;
|
2009-07-11 06:29:57 +00:00
|
|
|
docstring const oldname = qstring_to_ucs4(sel_index);
|
|
|
|
if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
|
|
|
|
if (newname.empty() || oldname == newname)
|
|
|
|
return;
|
2015-09-20 08:42:35 +00:00
|
|
|
bool success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
|
2009-04-16 07:29:01 +00:00
|
|
|
newIndexLE->clear();
|
|
|
|
updateView();
|
2009-08-27 16:47:45 +00:00
|
|
|
if (!success)
|
2017-07-03 17:53:14 +00:00
|
|
|
Alert::error(_("Renaming failed"),
|
2009-08-27 16:47:45 +00:00
|
|
|
_("The index could not be renamed. "
|
|
|
|
"Check if the new name already exists."));
|
2009-04-16 07:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
|
|
|
|
{
|
|
|
|
toggleColor(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-11 06:29:57 +00:00
|
|
|
void GuiIndices::on_indicesTW_itemSelectionChanged()
|
|
|
|
{
|
2015-06-03 14:54:54 +00:00
|
|
|
updateWidgets();
|
2009-07-11 06:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 07:29:01 +00:00
|
|
|
void GuiIndices::on_colorPB_clicked()
|
|
|
|
{
|
|
|
|
toggleColor(indicesTW->currentItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-03 14:54:54 +00:00
|
|
|
void GuiIndices::on_multipleIndicesCB_toggled(bool const b)
|
2009-04-16 07:29:01 +00:00
|
|
|
{
|
2015-06-03 14:54:54 +00:00
|
|
|
use_indices_ = b;
|
|
|
|
updateWidgets();
|
2009-04-16 07:29:01 +00:00
|
|
|
// emit signal
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiIndices::toggleColor(QTreeWidgetItem * item)
|
|
|
|
{
|
|
|
|
if (item == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString sel_index = item->text(0);
|
|
|
|
if (sel_index.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
docstring current_index = qstring_to_ucs4(sel_index);
|
|
|
|
Index * index = indiceslist_.find(current_index);
|
|
|
|
if (!index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QColor const initial = rgb2qcolor(index->color());
|
|
|
|
QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
|
|
|
|
if (!ncol.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// add the color to the indiceslist
|
|
|
|
index->setColor(fromqstr(ncol.name()));
|
|
|
|
newIndexLE->clear();
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
|
|
#include "moc_GuiIndices.cpp"
|