2007-04-25 16:39:21 +00:00
|
|
|
/*
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file QCitation.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
|
|
|
* \author Kalle Dalheimer
|
2007-03-25 01:32:12 +00:00
|
|
|
* \author Abdelrazak Younes
|
2006-03-05 17:24:44 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "QCitation.h"
|
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2006-03-25 21:26:09 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
using std::vector;
|
2006-03-25 21:26:09 +00:00
|
|
|
using std::string;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
template<typename String> static QStringList to_qstring_list(vector<String> const & v)
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2006-04-11 08:26:43 +00:00
|
|
|
QStringList qlist;
|
|
|
|
|
2007-04-25 16:39:21 +00:00
|
|
|
for (size_t i = 0; i != v.size(); ++i) {
|
2006-03-25 21:26:09 +00:00
|
|
|
if (v[i].empty())
|
|
|
|
continue;
|
2006-10-21 00:16:43 +00:00
|
|
|
qlist.append(lyx::toqstr(v[i]));
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
2006-04-11 08:26:43 +00:00
|
|
|
return qlist;
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
|
2007-04-25 16:39:21 +00:00
|
|
|
static vector<string> const to_string_vector(QStringList const & qlist)
|
2007-03-25 01:32:12 +00:00
|
|
|
{
|
|
|
|
vector<string> v;
|
2007-03-25 10:32:22 +00:00
|
|
|
for (int i = 0; i != qlist.size(); ++i) {
|
2007-03-25 01:32:12 +00:00
|
|
|
if (qlist[i].isEmpty())
|
|
|
|
continue;
|
|
|
|
v.push_back(lyx::fromqstr(qlist[i]));
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2006-03-25 21:26:09 +00:00
|
|
|
namespace lyx {
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace frontend {
|
|
|
|
|
2006-03-25 21:26:09 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
QCitation::QCitation(Dialog & parent)
|
2006-04-11 08:26:43 +00:00
|
|
|
: ControlCitation(parent)
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
void QCitation::apply(int const choice, bool const full, bool const force,
|
2006-12-10 10:31:42 +00:00
|
|
|
QString before, QString after)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
if (cited_keys_.isEmpty())
|
2006-06-28 08:28:16 +00:00
|
|
|
return;
|
2006-04-11 08:26:43 +00:00
|
|
|
|
|
|
|
vector<biblio::CiteStyle> const & styles =
|
|
|
|
ControlCitation::getCiteStyles();
|
|
|
|
|
|
|
|
string const command =
|
|
|
|
biblio::CitationStyle(styles[choice], full, force)
|
|
|
|
.asLatexStr();
|
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
params().setCmdName(command);
|
2007-03-25 01:32:12 +00:00
|
|
|
params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
|
2006-12-10 10:31:42 +00:00
|
|
|
params()["before"] = qstring_to_ucs4(before);
|
|
|
|
params()["after"] = qstring_to_ucs4(after);
|
2006-04-11 08:26:43 +00:00
|
|
|
dispatchParams();
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-15 09:58:44 +00:00
|
|
|
void QCitation::clearSelection()
|
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
cited_keys_.clear();
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-12-15 09:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
QString QCitation::textBefore()
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2006-12-10 10:31:42 +00:00
|
|
|
return toqstr(params()["before"]);
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
QString QCitation::textAfter()
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2006-12-10 10:31:42 +00:00
|
|
|
return toqstr(params()["after"]);
|
2006-04-11 08:26:43 +00:00
|
|
|
}
|
2006-04-05 23:56:29 +00:00
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
bool QCitation::initialiseParams(std::string const & data)
|
|
|
|
{
|
|
|
|
if (!ControlCitation::initialiseParams(data))
|
|
|
|
return false;
|
|
|
|
init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QCitation::init()
|
2006-04-11 08:26:43 +00:00
|
|
|
{
|
2006-03-25 21:26:09 +00:00
|
|
|
// Make the list of all available bibliography keys
|
2007-03-25 01:32:12 +00:00
|
|
|
all_keys_ = to_qstring_list(availableKeys());
|
|
|
|
available_model_.setStringList(all_keys_);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-03-25 21:26:09 +00:00
|
|
|
// Ditto for the keys cited in this inset
|
2006-12-10 10:31:42 +00:00
|
|
|
QString str = toqstr(params()["key"]);
|
2007-03-25 01:32:12 +00:00
|
|
|
if (str.isEmpty())
|
|
|
|
cited_keys_.clear();
|
|
|
|
else
|
|
|
|
cited_keys_ = str.split(",");
|
|
|
|
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-25 01:32:12 +00:00
|
|
|
void QCitation::findKey(QString const & str, bool only_keys,
|
|
|
|
bool case_sensitive, bool reg_exp)
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
if (str.isEmpty()) {
|
|
|
|
available_model_.setStringList(all_keys_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used for optimisation: store last searched string.
|
|
|
|
static QString last_searched_string;
|
|
|
|
// Used to disable the above optimisation.
|
|
|
|
static bool last_case_sensitive;
|
|
|
|
static bool last_reg_exp;
|
|
|
|
// Reset last_searched_string in case of changed option.
|
|
|
|
if (last_case_sensitive != case_sensitive
|
|
|
|
|| last_reg_exp != reg_exp) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI) << "QCitation::findKey: optimisation disabled!" << std::endl;
|
2007-03-25 01:32:12 +00:00
|
|
|
last_searched_string.clear();
|
|
|
|
}
|
|
|
|
// save option for next search.
|
|
|
|
last_case_sensitive = case_sensitive;
|
|
|
|
last_reg_exp = reg_exp;
|
|
|
|
|
|
|
|
Qt::CaseSensitivity qtcase = case_sensitive?
|
|
|
|
Qt::CaseSensitive: Qt::CaseInsensitive;
|
|
|
|
QStringList keys;
|
|
|
|
// If new string (str) contains the last searched one...
|
|
|
|
if (!last_searched_string.isEmpty() && str.size() > 1
|
|
|
|
&& str.contains(last_searched_string, qtcase))
|
|
|
|
// ... then only search within already found list.
|
|
|
|
keys = available_model_.stringList();
|
|
|
|
else
|
|
|
|
// ... else search all keys.
|
|
|
|
keys = all_keys_;
|
|
|
|
// save searched string for next search.
|
|
|
|
last_searched_string = str;
|
|
|
|
|
|
|
|
QStringList result;
|
|
|
|
if (only_keys)
|
|
|
|
// Search only within the matching keys:
|
|
|
|
result = keys.filter(str, qtcase);
|
|
|
|
else
|
|
|
|
// Search within matching keys and associated info.
|
|
|
|
result = to_qstring_list(searchKeys(to_string_vector(keys),
|
|
|
|
qstring_to_ucs4(str), case_sensitive, reg_exp));
|
|
|
|
|
|
|
|
available_model_.setStringList(result);
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
void QCitation::addKey(QModelIndex const & index)
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
cited_keys_.append(index.data().toString());
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
void QCitation::deleteKey(QModelIndex const & index)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
cited_keys_.removeAt(index.row());
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
void QCitation::upKey(QModelIndex const & index)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-28 08:28:16 +00:00
|
|
|
int pos = index.row();
|
2007-03-25 01:32:12 +00:00
|
|
|
cited_keys_.swap(pos, pos - 1);
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-03-25 21:26:09 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-11 08:26:43 +00:00
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
void QCitation::downKey(QModelIndex const & index)
|
2006-03-25 21:26:09 +00:00
|
|
|
{
|
2006-06-28 08:28:16 +00:00
|
|
|
int pos = index.row();
|
2007-03-25 01:32:12 +00:00
|
|
|
cited_keys_.swap(pos, pos + 1);
|
|
|
|
selected_model_.setStringList(cited_keys_);
|
2006-04-11 08:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QStringList QCitation::citationStyles(int sel)
|
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
string const key = fromqstr(cited_keys_[sel]);
|
|
|
|
return to_qstring_list(getCiteStrings(key));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 08:28:16 +00:00
|
|
|
|
|
|
|
QString QCitation::getKeyInfo(QString const & sel)
|
|
|
|
{
|
2007-03-25 01:32:12 +00:00
|
|
|
return toqstr(getInfo(fromqstr(sel)));
|
2006-06-28 08:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|