00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <string>
00022 #include <iostream>
00023
00024 #include <QtGui/QFileDialog>
00025 #include <QtGui/QMessageBox>
00026 #include <QtGui/QTextBrowser>
00027 #include <QtGui/QToolButton>
00028
00029 #include "phrasehunter/corpus.h"
00030 #include "phrasehunter/phexception.h"
00031 #include "phrasehunter/contextreader.h"
00032
00033 #include "phsearchimpl.h"
00034 #include "searchtab.h"
00035 #include "textbrowsertab.h"
00036
00037
00038 PhSearchImpl::PhSearchImpl()
00039 : m_corpus(NULL)
00040 {
00041 setupUi(this);
00042 setupTabIcons();
00043 setupConnections();
00044
00045 tabWidget->removeTab(0);
00046 newSearchTab();
00047 hideStatistics();
00048
00049
00050 searchKindComboBox->removeItem(2);
00051 }
00052
00053 void PhSearchImpl::setupConnections()
00054 {
00055 connect(action_Open, SIGNAL(triggered()), this, SLOT(fileOpen()));
00056 connect(action_Exit, SIGNAL(triggered()), this, SLOT(fileExit()));
00057 connect(actionSearch_Options, SIGNAL(triggered()), this, SLOT(openOptionsWidget()));
00058 connect(actionNew_Search_Tab, SIGNAL(triggered()), this, SLOT(newSearchTab()));
00059 connect(searchKindComboBox, SIGNAL(currentIndexChanged(const QString& )), this, SLOT(hideStatistics()));
00060 }
00061
00062 PhSearchImpl::~PhSearchImpl()
00063 {
00064 deleteCorpus();
00065 }
00066
00067 void PhSearchImpl::deleteCorpus()
00068 {
00069 delete m_corpus;
00070 m_corpus = NULL;
00071 }
00072
00073 void PhSearchImpl::fileOpen()
00074 {
00075 try {
00076 deleteCorpus();
00077 QString corpusDir =
00078 QFileDialog::getExistingDirectory(this,
00079 QString("Choose a Corpus"),
00080 QDir::homePath(),
00081 QFileDialog::ShowDirsOnly);
00082 if(corpusDir.isEmpty())
00083 return;
00084
00085 m_corpus = new PhraseHunter::CorpusManager(corpusDir.toStdString());
00086 m_corpus->statisticsEngine()->setNumberContextWords(40);
00087
00088 QList<SearchTab*> searchLines = tabWidget->findChildren<SearchTab*>();
00089 for (int i = 0; i < searchLines.size(); ++i) {
00090 searchLines.at(i)->enableSearch(m_corpus);
00091 }
00092 }
00093 catch(PhraseHunter::Exceptions::NoSuchCorpusError& _e) {
00094 QMessageBox::critical(this, QString("Corpus Error"), QString("This directory does not contain a valid corpus: ")+QString(_e.what()),QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
00095 fileOpen();
00096 }
00097 catch (PhraseHunter::Exceptions::Exception& _e) {
00098 QMessageBox::critical(this, QString("Error"), QString(_e.what()),QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton );
00099 }
00100 }
00101
00102 void PhSearchImpl::fileExit()
00103 {
00104 close();
00105 }
00106
00107 void PhSearchImpl::openOptionsWidget()
00108 {
00109 optionsDockWidget->setVisible(TRUE);
00110 }
00111
00112 void PhSearchImpl::newSearchTab()
00113 {
00114 SearchTab* newTab = new SearchTab(this);
00115 tabWidget->addTab(newTab, QApplication::translate("phMainWindow", "Query", 0,
00116 QApplication::UnicodeUTF8));
00117 tabWidget->setCurrentWidget(newTab);
00118 if(m_corpus != NULL) {
00119 newTab->enableSearch(m_corpus);
00120 }
00121
00122 newTab->setQueryType(searchKindComboBox->currentText());
00123
00124 connect(searchKindComboBox, SIGNAL(currentIndexChanged(const QString&)),
00125 newTab, SLOT(setQueryType(const QString&)));
00126
00127 newTab->setContextWidth(contextWidthSpinBox->value());
00128
00129 connect(contextWidthSpinBox, SIGNAL(valueChanged(int)),
00130 newTab, SLOT(setContextWidth(int)));
00131
00132 connect(newTab, SIGNAL(documentRequested(PhraseHunter::DocID, PhraseHunter::IdxPos, size_t)),
00133 this, SLOT(openDocument(PhraseHunter::DocID, PhraseHunter::IdxPos, size_t)));
00134
00135 }
00136
00137 void PhSearchImpl::setupTabIcons()
00138 {
00139 QToolButton *closeTabButton = new QToolButton(tabWidget);
00140 tabWidget->setCornerWidget(closeTabButton, Qt::TopRightCorner);
00141 closeTabButton->setAutoRaise(true);
00142 closeTabButton->setIcon(QIcon(":/icons/closetab.png"));
00143 closeTabButton->setToolTip(tr("Close page"));
00144 closeTabButton->setEnabled(true);
00145
00146 QToolButton *newTabButton = new QToolButton(tabWidget);
00147 tabWidget->setCornerWidget(newTabButton, Qt::TopLeftCorner);
00148 newTabButton->setAutoRaise(true);
00149 newTabButton->setIcon(QIcon(":/icons/addtab.png"));
00150 newTabButton->setToolTip(tr("New page"));
00151 newTabButton->setEnabled(true);
00152
00153 connect(closeTabButton, SIGNAL(clicked()), this, SLOT(closeTab()));
00154 connect(newTabButton, SIGNAL(clicked()), this, SLOT(newSearchTab()));
00155 }
00156
00157
00158 void PhSearchImpl::hideStatistics()
00159 {
00160 sortByRankCheckBox->setHidden(true);
00161
00162
00163
00164
00165
00166 }
00167
00168 void PhSearchImpl::closeTab()
00169 {
00170 if(tabWidget->count() > 1)
00171 tabWidget->removeTab(tabWidget->currentIndex());
00172 }
00173
00174 void PhSearchImpl::helpAbout()
00175 {
00176 qWarning( "PhSearchImpl::helpAbout() not yet implemented!" );
00177 }
00178
00179 void PhSearchImpl::openDocument(PhraseHunter::DocID docID, PhraseHunter::IdxPos position, size_t tokenLength)
00180 {
00181 PhraseHunter::TokenContextPtr output =
00182 m_corpus->contextReader()->getContextFromPosition(docID, position, tokenLength, sourceContextSpinBox->value());
00183
00184 TextBrowserTab* textBrowserTab = new TextBrowserTab(this, output);
00185
00186 tabWidget->addTab(textBrowserTab, QString("<S>") +
00187 tabWidget->currentWidget()->findChild<QLineEdit*>("leQuery")->text());
00188
00189 tabWidget->setCurrentWidget(textBrowserTab);
00190 }
00191
00192 int main(int argc, char* argv[])
00193 {
00194 try {
00195 QApplication a( argc, argv );
00196 PhSearchImpl searcher;
00197 searcher.show();
00198 return a.exec();
00199 }
00200 catch (PhraseHunter::Exceptions::Exception& _e) {
00201 QMessageBox::critical(NULL, QString("Error"), QString(_e.what()),QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton );
00202 }
00203 catch (...) {
00204 QMessageBox::critical(NULL, QString("error"),
00205 QString("An unexpected error occurred"), QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
00206 }
00207 }