| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE SearchDialog.cc |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <QFileDialog> |
| 29 |
#include <algorithm> |
| 30 |
#include <regex> |
| 31 |
#include <stack> |
| 32 |
|
| 33 |
#include "ApvlvFile.h" |
| 34 |
#include "ApvlvSearchDialog.h" |
| 35 |
|
| 36 |
namespace apvlv |
| 37 |
{ |
| 38 |
using namespace std; |
| 39 |
|
| 40 |
SearchDialog::SearchDialog (QWidget *parent) |
| 41 |
: QDialog (parent), mPreviewIsFinished (true) |
| 42 |
{ |
| 43 |
setLayout (&mVBox); |
| 44 |
|
| 45 |
mVBox.addLayout (&mHBox2); |
| 46 |
mLabel.setText (tr ("Find Directory: ")); |
| 47 |
mHBox2.addWidget (&mLabel, 0); |
| 48 |
mHBox2.addWidget (&mFromDir, 1); |
| 49 |
QObject::connect (&mFromDir, SIGNAL (returnPressed ()), this, |
| 50 |
SLOT (search ())); |
| 51 |
mFromDir.setText (QDir::homePath ()); |
| 52 |
mDirButton.setText (tr ("...")); |
| 53 |
mDirButton.setFocusPolicy (Qt::NoFocus); |
| 54 |
mHBox2.addWidget (&mDirButton, 0); |
| 55 |
QObject::connect (&mDirButton, &QPushButton::clicked, this, [&] () { |
| 56 |
auto dir = QFileDialog::getExistingDirectory (); |
| 57 |
if (!dir.isEmpty ()) |
| 58 |
mFromDir.setText (dir); |
| 59 |
}); |
| 60 |
|
| 61 |
// search line |
| 62 |
mVBox.addLayout (&mHBox); |
| 63 |
|
| 64 |
mHBox.addWidget (&mSearchEdit, 1); |
| 65 |
QObject::connect (&mSearchEdit, SIGNAL (returnPressed ()), this, |
| 66 |
SLOT (search ())); |
| 67 |
|
| 68 |
mCaseSensitive.setText (tr ("Case sensitive")); |
| 69 |
mHBox.addWidget (&mCaseSensitive); |
| 70 |
|
| 71 |
mRegex.setText (tr ("Regular expression")); |
| 72 |
mHBox.addWidget (&mRegex); |
| 73 |
|
| 74 |
// file type line |
| 75 |
mVBox.addLayout (&mHBox3); |
| 76 |
|
| 77 |
auto exts = FileFactory::supportFileExts (); |
| 78 |
std::ranges::for_each (exts, [&] (const auto &ext) { |
| 79 |
auto checkbox = new QCheckBox (QString::fromLocal8Bit (ext)); |
| 80 |
checkbox->setChecked (true); |
| 81 |
mHBox3.addWidget (checkbox); |
| 82 |
mTypes.emplace_back (checkbox); |
| 83 |
}); |
| 84 |
|
| 85 |
mVBox.addWidget (&mSplitter); |
| 86 |
|
| 87 |
mSplitter.setOrientation (Qt::Vertical); |
| 88 |
|
| 89 |
mSplitter.addWidget (&mResults); |
| 90 |
mSplitter.addWidget (&mPreview); |
| 91 |
mPreview.resize (400, 300); |
| 92 |
QObject::connect ( |
| 93 |
&mResults, |
| 94 |
SIGNAL (currentItemChanged (QListWidgetItem *, QListWidgetItem *)), this, |
| 95 |
SLOT (previewItem (QListWidgetItem *))); |
| 96 |
QObject::connect (&mResults, SIGNAL (itemActivated (QListWidgetItem *)), |
| 97 |
this, SLOT (activateItem (QListWidgetItem *))); |
| 98 |
QObject::connect (&mPreview, SIGNAL (loadFinished (bool)), this, |
| 99 |
SLOT (loadFinish (bool))); |
| 100 |
|
| 101 |
QObject::connect (&mGetTimer, SIGNAL (timeout ()), this, |
| 102 |
SLOT (getResults ())); |
| 103 |
mGetTimer.start (100); |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
SearchDialog::search () |
| 108 |
{ |
| 109 |
SearchOptions options; |
| 110 |
options.mText = mSearchEdit.text ().trimmed ().toStdString (); |
| 111 |
options.mCaseSensitive = mCaseSensitive.isChecked (); |
| 112 |
options.mRegex = mRegex.isChecked (); |
| 113 |
options.mFromDir = mFromDir.text ().toStdString (); |
| 114 |
for (const auto &type : mTypes) |
| 115 |
{ |
| 116 |
auto ext = type->text ().replace ("&", ""); |
| 117 |
if (type->isChecked ()) |
| 118 |
options.mTypes.emplace_back (ext.toStdString ()); |
| 119 |
} |
| 120 |
if (mOptions == options) |
| 121 |
return; |
| 122 |
|
| 123 |
mSearcher.submit (options); |
| 124 |
mResults.clear (); |
| 125 |
mOptions = options; |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
SearchDialog::getResults () |
| 130 |
{ |
| 131 |
unique_ptr<SearchFileMatch> result; |
| 132 |
while ((result = mSearcher.get ()) != nullptr) |
| 133 |
{ |
| 134 |
displayResult (std::move (result)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
SearchDialog::previewItem (QListWidgetItem *item) |
| 140 |
{ |
| 141 |
if (item == nullptr) |
| 142 |
return; |
| 143 |
|
| 144 |
if (mPreviewIsFinished == false) |
| 145 |
return; |
| 146 |
|
| 147 |
auto words = item->data (Qt::UserRole).toStringList (); |
| 148 |
auto path = words[0].toStdString (); |
| 149 |
auto pn = words[1].toInt (); |
| 150 |
if (mPreviewFile && mPreviewFile->getFilename () != path) |
| 151 |
mPreviewFile = nullptr; |
| 152 |
|
| 153 |
if (mPreviewFile == nullptr) |
| 154 |
mPreviewFile = FileFactory::loadFile (path); |
| 155 |
|
| 156 |
if (mPreviewFile) |
| 157 |
{ |
| 158 |
mPreview.setFile (mPreviewFile.get ()); |
| 159 |
mPreviewIsFinished = false; |
| 160 |
mPreviewFile->pageRenderToWebView (pn, 1.0, 0, &mPreview); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
SearchDialog::activateItem (QListWidgetItem *item) |
| 166 |
{ |
| 167 |
auto words = item->data (Qt::UserRole).toStringList (); |
| 168 |
auto path = words[0]; |
| 169 |
auto pn = words[1].toInt (); |
| 170 |
emit loadFile (path.toStdString (), pn); |
| 171 |
// accept (); |
| 172 |
} |
| 173 |
|
| 174 |
void |
| 175 |
SearchDialog::displayResult (unique_ptr<SearchFileMatch> result) |
| 176 |
{ |
| 177 |
auto line = QString::fromLocal8Bit (result->filename); |
| 178 |
for (const auto &page : result->page_matches) |
| 179 |
{ |
| 180 |
auto pos = line + ':' + QString::number (page.page + 1); |
| 181 |
for_each (page.matches.begin (), page.matches.end (), |
| 182 |
[&] (const auto &match) { |
| 183 |
auto matchitem = new QListWidgetItem ( |
| 184 |
{ QString::fromLocal8Bit (match.line) }); |
| 185 |
matchitem->setToolTip (pos); |
| 186 |
QStringList data{ line, QString::number (page.page) }; |
| 187 |
matchitem->setData (Qt::UserRole, data); |
| 188 |
mResults.addItem (matchitem); |
| 189 |
}); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void |
| 194 |
SearchDialog::loadFinish ([[maybe_unused]] bool ret) |
| 195 |
{ |
| 196 |
mPreviewIsFinished = true; |
| 197 |
} |
| 198 |
|
| 199 |
} |