SearchTab.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "SearchModel.h"
00025 #include "SearchTab.h"
00026 
00027 using UIs::Searching::SearchTab;
00028 using UIs::Searching::SearchTreeView;
00029 using UIs::Searching::SearchResult;
00030 
00031 // \todo Fix these warnings:
00032 // UIs/Searching/SearchTab.cpp(31): warning #1944: declaration of "localPeer" shadows a member of 'this'
00033 // UIs/Searching/SearchTab.cpp(31): warning #1944: declaration of "tabWidget" shadows a member of 'this'
00034 SearchTab::SearchTab (SearchTreeView *searchTreeView, LocalPeer *localPeer, QTabWidget *tabWidget, QWidget *parent)
00035  :  QWidget (parent), treeView (searchTreeView), tabWidget (tabWidget),
00036     searchModel (new SearchModel (searchTreeView)),
00037     buttonLayout (0), queryView (0), searchLayout (new QHBoxLayout),
00038     localPeer (localPeer), wasActive (true), isSearching (false)
00039 {
00040     QObject::connect (searchModel, SIGNAL (itemAdded()), this, SLOT (itemAdded()));
00041     QObject::connect (tabWidget, SIGNAL (currentChanged(int)), this, SLOT (currentTabChanged(int)));
00042 
00043     searchTreeView->setModel (searchModel);
00044     queryView = new SearchQueryView(this);
00045 
00046     QObject::connect(queryView, SIGNAL(startSearch()), this, SLOT(startSearch()));
00047     QObject::connect(queryView, SIGNAL(stopSearch()), this, SLOT(stopSearch()));
00048 
00049     searchLayout->setMargin (0);
00050     searchLayout->addSpacing (4);
00051 
00052     searchLayout->addLayout (queryView, 1);
00053     searchLayout->addWidget (searchTreeView, 5);
00054 
00055     setLayout (searchLayout);
00056 }
00057 SearchTab::~SearchTab()
00058 {
00059     delete searchModel;
00060     delete queryView;
00061     delete searchLayout;
00062 }
00063 
00064 void SearchTab::startSearch()
00065 {
00066     QString sText   = queryView->query();
00067     QString tabText = sText;
00068 
00069     if (tabText.length() > MAX_TABTEXT_LEN) {
00070         tabText.remove (MAX_TABTEXT_LEN - 3, tabText.length());
00071         tabText.append (QString ("..."));
00072     }
00073 
00074     //tabText.append (QString (" (0 found)"));
00075     tabWidget->setTabText (tabWidget->indexOf (this), tabText);
00076 
00077     localPeer->startSearch (searchModel, sText);
00078     isSearching = true;
00079 }
00080 
00081 void SearchTab::stopSearch()
00082 {
00083     if (isSearching) {
00084         localPeer->stopSearch (searchModel);
00085         isSearching = false;
00086     }
00087 }
00088 
00089 void SearchTab::itemAdded()
00090 {
00091     QString tabText = tabWidget->tabText(tabWidget->indexOf (this));
00092 
00093     int start_additional_text = tabText.lastIndexOf ('(');
00094     if (start_additional_text != -1) {
00095         tabText.remove (start_additional_text, tabText.length());
00096     }
00097     if (tabText[tabText.length() - 1] != ' ') tabText.append (" ");
00098     tabText.append (QString().sprintf ("(%d found)", searchModel->rowCount()));
00099 
00100     tabWidget->setTabText (tabWidget->indexOf (this), tabText);
00101 }
00102 
00103 void SearchTab::currentTabChanged (int index)
00104 {
00105     if (index == tabWidget->indexOf (this)) {
00106         wasActive = true;
00107         searchModel->tabActivated();
00108     } else if (wasActive){
00109         wasActive = false;
00110         searchModel->tabDisactivated();
00111     }
00112 }