1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/************************************************************************
**
** Copyright (C) 2015-2021 Kevin B. Hendricks, Stratford Ontario Canada
** Copyright (C) 2012 John Schember <john@nachtimwald.com>
** Copyright (C) 2012 Dave Heiland
**
** This file is part of PageEdit.
**
** PageEdit is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** PageEdit is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "SettingsStore.h"
#include "GumboInterface.h"
#include "Utility.h"
#include "SelectHyperlink.h"
#include "pageedit_constants.h"
static QString SETTINGS_GROUP = "select_hyperlink";
SelectHyperlink::SelectHyperlink(QString default_href, QString &source, QString ¤tpath,
QString &base, QStringList &other_files, QWidget *parent)
:
QDialog(parent),
m_DefaultTarget(default_href),
m_Source(source),
m_CurrentFile(currentpath),
m_Base(base),
m_OtherFiles(other_files),
m_SavedTarget(QString()),
m_SelectHyperlinkModel(new QStandardItemModel)
{
ui.setupUi(this);
connectSignalsSlots();
ReadSettings();
SetList();
// Set default href name
ui.href->setText(m_DefaultTarget);
if (!m_DefaultTarget.isEmpty()) {
SelectText(m_DefaultTarget);
} else {
SelectText(m_SavedTarget);
}
// Default entry on the target url not the filter
ui.href->setFocus();
}
QList<SelectHyperlink::TargetInfo> SelectHyperlink::CollectTargets(const QString& source, const QString& filepath)
{
QList<SelectHyperlink::TargetInfo> targets;
GumboInterface gi(source, "any_version");
QList<GumboNode*> nodes = gi.get_all_nodes_with_attribute(QString("id"));
nodes.append(gi.get_all_nodes_with_attribute(QString("name")));
foreach(GumboNode * node, nodes) {
SelectHyperlink::TargetInfo atarget;
atarget.element = QString::fromStdString(gi.get_tag_name(node));
atarget.filename = filepath;
atarget.text = gi.get_local_text_of_node(node);
GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "id");
if (attr) {
atarget.id = QString::fromUtf8(attr->value);
} else {
// This is supporting legacy html of <a name="xxx"> (deprecated).
// Make sure we don't return names of other elements like <meta> tags.
if (atarget.element == "a") {
attr = gumbo_get_attribute(&node->v.element.attributes, "name");
if (attr) {
atarget.id = QString::fromUtf8(attr->value);
}
}
}
if (atarget.id != "PageEdit_Injected") {
targets << atarget;
}
}
return targets;
}
void SelectHyperlink::SetList()
{
m_SelectHyperlinkModel->clear();
QStringList header;
header.append(tr("Targets in the Book"));
header.append(tr("Text"));
m_SelectHyperlinkModel->setHorizontalHeaderLabels(header);
ui.list->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.list->setModel(m_SelectHyperlinkModel);
// first get the list of valid targets in the current source file
QList<SelectHyperlink::TargetInfo> targets = CollectTargets(m_Source, m_CurrentFile);
AddEntry(targets);
targets.clear();
// then build the list of targets for the other source files
foreach(QString apath, m_OtherFiles) {
QString fullpath = m_Base + apath;
QString source;
try {
source = Utility::ReadUnicodeTextFile(fullpath);
} catch (std::exception &e) {
source = "<html><head><title></title></head><body></body></html>";
}
targets = CollectTargets(source, apath);
AddEntry(targets);
targets.clear();
}
ui.list->resizeColumnToContents(0);
ui.list->resizeColumnToContents(1);
}
void SelectHyperlink::AddEntry(const QList<SelectHyperlink::TargetInfo> &targets)
{
foreach(SelectHyperlink::TargetInfo tinfo, targets) {
QString target;
QString id = tinfo.id;
QString fragment;
if (!id.isEmpty()) {
fragment = "#" + id;
}
if (m_CurrentFile == tinfo.filename) {
// link is local to this file
target = fragment;
if (target.isEmpty()) target = "#";
} else {
target = tinfo.filename + fragment;
}
// abbreviate any local text
QString text = tinfo.text;
text = text.left(50);
QString filepath = m_Base + tinfo.filename + fragment;
QList<QStandardItem *> rowItems;
QStandardItem *target_item = new QStandardItem();
target_item->setText(target);
target_item->setData(filepath);
rowItems << target_item;
target_item = new QStandardItem();
target_item->setText(text);
rowItems << target_item;
m_SelectHyperlinkModel->appendRow(rowItems);
rowItems[0]->setEditable(false);
rowItems[1]->setEditable(false);
}
}
QString SelectHyperlink::GetSelectedText()
{
QString text;
if (ui.list->selectionModel()->hasSelection()) {
QModelIndexList selected_indexes = ui.list->selectionModel()->selectedRows(0);
if (!selected_indexes.isEmpty()) {
QStandardItem *item = m_SelectHyperlinkModel->itemFromIndex(selected_indexes.last());
if (item) {
text = item->text();
}
}
}
return text;
}
void SelectHyperlink::SelectText(QString &text)
{
if (!text.isEmpty()) {
QModelIndex parent_index;
QStandardItem *root_item = m_SelectHyperlinkModel->invisibleRootItem();
// Convert search text to filename#fragment
QString target = text;
if (target.startsWith("#") && !m_CurrentFile.isEmpty()) {
target = m_CurrentFile + text;
}
if (target.contains("/")) {
target = target.right(target.length() - target.lastIndexOf("/") - 1);
}
for (int row = 0; row < root_item->rowCount(); row++) {
QStandardItem *child = root_item->child(row, 0);
// Convert selection text to filename#fragment
QString selection = child->data().toString();
if (selection.contains("/")) {
selection = selection.right(selection.length() - selection.lastIndexOf("/") - 1);
}
if (target == selection) {
ui.list->selectionModel()->select(m_SelectHyperlinkModel->index(row, 0, parent_index), QItemSelectionModel::Select | QItemSelectionModel::Rows);
ui.list->setFocus();
ui.list->setCurrentIndex(child->index());
}
}
}
}
void SelectHyperlink::FilterEditTextChangedSlot(const QString &text)
{
const QString lowercaseText = text.toLower();
QStandardItem *root_item = m_SelectHyperlinkModel->invisibleRootItem();
QModelIndex parent_index;
// Hide rows that don't contain the filter text
int first_visible_row = -1;
for (int row = 0; row < root_item->rowCount(); row++) {
if (text.isEmpty() || root_item->child(row, 0)->text().toLower().contains(lowercaseText)) {
ui.list->setRowHidden(row, parent_index, false);
if (first_visible_row == -1) {
first_visible_row = row;
}
} else {
ui.list->setRowHidden(row, parent_index, true);
}
}
if (!text.isEmpty() && first_visible_row != -1) {
// Select the first non-hidden row
ui.list->setCurrentIndex(root_item->child(first_visible_row, 0)->index());
} else {
// Clear current and selection, which clears preview image
ui.list->setCurrentIndex(QModelIndex());
}
}
QString SelectHyperlink::GetTarget()
{
QString target;
target = ui.href->text();
return target;
}
void SelectHyperlink::DoubleClicked(const QModelIndex &index)
{
Clicked(index);
accept();
}
void SelectHyperlink::Clicked(const QModelIndex &index)
{
QStandardItem *item = m_SelectHyperlinkModel->itemFromIndex(index);
if (item->text().startsWith("#")) {
ui.href->setText(m_CurrentFile + item->text());
} else {
ui.href->setText(item->text());
// ui.href->setText(item->data().toString());
}
}
void SelectHyperlink::ReadSettings()
{
SettingsStore settings;
settings.beginGroup(SETTINGS_GROUP);
// The size of the window and it's full screen status
QByteArray geometry = settings.value("geometry").toByteArray();
if (!geometry.isNull()) {
restoreGeometry(geometry);
}
m_SavedTarget = settings.value("lastselectedentry").toString();
settings.endGroup();
}
void SelectHyperlink::WriteSettings()
{
SettingsStore settings;
settings.beginGroup(SETTINGS_GROUP);
// The size of the window and it's full screen status
settings.setValue("geometry", saveGeometry());
settings.setValue("lastselectedentry", GetSelectedText());
settings.endGroup();
}
void SelectHyperlink::connectSignalsSlots()
{
connect(this, SIGNAL(accepted()), this, SLOT(WriteSettings()));
connect(ui.filter, SIGNAL(textChanged(QString)), this, SLOT(FilterEditTextChangedSlot(QString)));
connect(ui.list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(DoubleClicked(const QModelIndex &)));
connect(ui.list, SIGNAL(clicked(const QModelIndex &)), this, SLOT(Clicked(const QModelIndex &)));
}
|