KDevelop API Documentation

parts/grepview/grepviewwidget.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 1999-2001 by Bernd Gehrmann * 00003 * bernd@kdevelop.org * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include <qdir.h> 00013 #include <qlayout.h> 00014 #include <qregexp.h> 00015 #include <qpainter.h> 00016 #include <kdialogbase.h> 00017 #include <klocale.h> 00018 #include <kprocess.h> 00019 #include <kparts/part.h> 00020 #include <ktexteditor/selectioninterface.h> 00021 using namespace KTextEditor; 00022 00023 #include "kdevcore.h" 00024 #include "kdevproject.h" 00025 #include "kdevmainwindow.h" 00026 #include "kdevpartcontroller.h" 00027 00028 #include "grepdlg.h" 00029 #include "grepviewpart.h" 00030 #include "grepviewwidget.h" 00031 00032 00033 class GrepListBoxItem : public ProcessListBoxItem 00034 { 00035 public: 00036 GrepListBoxItem(const QString &fileName, const QString &lineNumber, const QString &text, bool showFilename); 00037 QString filename() 00038 { return fileName; } 00039 int linenumber() 00040 { return lineNumber.toInt(); } 00041 virtual bool isCustomItem(); 00042 00043 private: 00044 virtual void paint(QPainter *p); 00045 QString fileName, lineNumber, text; 00046 bool show; 00047 }; 00048 00049 00050 GrepListBoxItem::GrepListBoxItem(const QString &fileName, const QString &lineNumber, const QString &text, bool showFilename) 00051 : ProcessListBoxItem( QString::null, Normal), 00052 fileName(fileName), lineNumber(lineNumber), text(text.stripWhiteSpace()), 00053 show(showFilename) 00054 {} 00055 00056 00057 bool GrepListBoxItem::isCustomItem() 00058 { 00059 return true; 00060 } 00061 00062 00063 void GrepListBoxItem::paint(QPainter *p) 00064 { 00065 QFontMetrics fm = p->fontMetrics(); 00066 QString stx = lineNumber + ": "; 00067 int y = fm.ascent()+fm.leading()/2; 00068 int x = 3; 00069 if (show) 00070 { 00071 p->setPen(Qt::darkGreen); 00072 p->drawText(x, y, fileName); 00073 x += fm.width(fileName); 00074 } 00075 else { 00076 p->setPen(Qt::black); 00077 QFont font1(p->font()); 00078 QFont font2(font1); 00079 font2.setBold(true); 00080 p->setFont(font2); 00081 p->drawText(x, y, stx); 00082 p->setFont(font1); 00083 x += fm.width(stx); 00084 00085 p->setPen(Qt::blue); 00086 p->drawText(x, y, text); 00087 } 00088 } 00089 00090 00091 GrepViewWidget::GrepViewWidget(GrepViewPart *part) 00092 : ProcessWidget(0, "grep widget") 00093 , m_matchCount(0) 00094 { 00095 grepdlg = new GrepDialog(this, "grep widget"); 00096 connect( grepdlg, SIGNAL(searchClicked()), 00097 this, SLOT(searchActivated()) ); 00098 connect( this, SIGNAL(clicked(QListBoxItem*)), 00099 this, SLOT(slotExecuted(QListBoxItem*)) ); 00100 connect( this, SIGNAL(returnPressed(QListBoxItem*)), 00101 this, SLOT(slotExecuted(QListBoxItem*)) ); 00102 00103 m_part = part; 00104 } 00105 00106 00107 GrepViewWidget::~GrepViewWidget() 00108 {} 00109 00110 00111 void GrepViewWidget::showDialog() 00112 { 00113 // Get the selected text if there is any 00114 KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(m_part->partController()->activePart()); 00115 if (ro_part) 00116 { 00117 SelectionInterface *selectIface = dynamic_cast<SelectionInterface*>(ro_part); 00118 if(selectIface && selectIface->hasSelection()) 00119 { 00120 QString selText = selectIface->selection(); 00121 if(!selText.contains('\n')) 00122 { 00123 grepdlg->setPattern(selText); 00124 } 00125 } 00126 } 00127 grepdlg->show(); 00128 } 00129 00130 // @todo - put this somewhere common - or just use Qt if possible 00131 static QString escape(const QString &str) 00132 { 00133 QString escaped("[]{}()\\^$?.+-*"); 00134 QString res; 00135 00136 for (uint i=0; i < str.length(); ++i) { 00137 if (escaped.find(str[i]) != -1) 00138 res += "\\"; 00139 res += str[i]; 00140 } 00141 00142 return res; 00143 } 00144 00145 00146 void GrepViewWidget::showDialogWithPattern(QString pattern) 00147 { 00148 // Before anything, this removes line feeds from the 00149 // beginning and the end. 00150 int len = pattern.length(); 00151 if (len > 0 && pattern[0] == '\n') 00152 { 00153 pattern.remove(0, 1); 00154 len--; 00155 } 00156 if (len > 0 && pattern[len-1] == '\n') 00157 pattern.truncate(len-1); 00158 grepdlg->setPattern( pattern ); 00159 grepdlg->show(); 00160 } 00161 00162 00163 void GrepViewWidget::searchActivated() 00164 { 00165 m_matchCount = 0; 00166 _lastfilename = ""; 00167 00168 QString files; 00169 // waba: code below breaks on filenames containing a ',' !!! 00170 QStringList filelist = QStringList::split(",", grepdlg->filesString()); 00171 if (!filelist.isEmpty()) 00172 { 00173 QStringList::Iterator it(filelist.begin()); 00174 files = KShellProcess::quote(*it); 00175 ++it; 00176 for (; it != filelist.end(); ++it) 00177 files += " -o -name " + KShellProcess::quote(*it); 00178 } 00179 00180 QString pattern = grepdlg->templateString(); 00181 // pattern.replace(QRegExp("%s"), grepdlg->patternString()); 00182 pattern.replace(QRegExp("%s"), escape( grepdlg->patternString() ) ); 00183 pattern.replace(QRegExp("'"), "'\\''"); 00184 00185 QString filepattern = "find "; 00186 filepattern += KShellProcess::quote(grepdlg->directoryString()); 00187 if (!grepdlg->recursiveFlag()) 00188 filepattern += " -maxdepth 1"; 00189 filepattern += " \\( -name "; 00190 filepattern += files; 00191 filepattern += " \\) -print -follow"; 00192 00193 QString command = filepattern + " " ; 00194 if (grepdlg->ignoreSCMDirsFlag()) { 00195 command += "| grep -v \"SCCS/\" "; 00196 command += "| grep -v \"CVS/\" "; 00197 } 00198 00199 // quote spaces in filenames going to xargs 00200 command += "| sed \"s/ /\\\\\\ /g\" "; 00201 00202 command += "| xargs " ; 00203 00204 #ifndef USE_SOLARIS 00205 command += "egrep -H -n -e "; 00206 #else 00207 // -H reported as not being available on Solaris, 00208 // but we're buggy without it on Linux. 00209 command += "egrep -n -e "; 00210 #endif 00211 command += KShellProcess::quote(pattern); 00212 startJob("", command); 00213 00214 m_part->mainWindow()->raiseView(this); 00215 m_part->core()->running(m_part, true); 00216 } 00217 00218 00219 void GrepViewWidget::childFinished(bool normal, int status) 00220 { 00221 // When xargs executes grep several times (because the command line 00222 // generated would be too large for a single grep) and one of those 00223 // sets of files passed to grep does not contain a match, then an 00224 // error status of 123 is set by xargs, even if there is a match in 00225 // another set of files. 00226 // Reset this false status here. 00227 if (status == 123 && numRows() > 1) 00228 status = 0; 00229 00230 insertItem(new ProcessListBoxItem(i18n("*** %n match found. ***", "*** %n matches found. ***", m_matchCount), ProcessListBoxItem::Diagnostic)); 00231 maybeScrollToBottom(); 00232 00233 ProcessWidget::childFinished(normal, status); 00234 m_part->core()->running(m_part, false); 00235 } 00236 00237 00238 void GrepViewWidget::slotExecuted(QListBoxItem* item) 00239 { 00240 ProcessListBoxItem *i = static_cast<ProcessListBoxItem*>(item); 00241 if (!i || !i->isCustomItem()) 00242 return; 00243 00244 GrepListBoxItem *gi = static_cast<GrepListBoxItem*>(i); 00245 m_part->partController()->editDocument( KURL( gi->filename() ), gi->linenumber()-1 ); 00246 // m_part->mainWindow()->lowerView(this); 00247 } 00248 00249 00250 void GrepViewWidget::insertStdoutLine(const QString &line) 00251 { 00252 int pos; 00253 QString filename, linenumber, rest; 00254 00255 QString str = line; 00256 if ( (pos = str.find(':')) != -1) 00257 { 00258 filename = str.left(pos); 00259 str.remove( 0, pos+1 ); 00260 if ( ( pos = str.find(':') ) != -1) 00261 { 00262 linenumber = str.left(pos); 00263 str.remove( 0, pos+1 ); 00264 // filename will be displayed only once 00265 // selecting filename will display line 1 of file, 00266 // otherwise, line of requested search 00267 if ( _lastfilename != filename ) 00268 { 00269 _lastfilename = filename; 00270 insertItem(new GrepListBoxItem(filename, "0", str, true)); 00271 insertItem(new GrepListBoxItem(filename, linenumber, str, false)); 00272 } 00273 else 00274 { 00275 insertItem(new GrepListBoxItem(filename, linenumber, str, false)); 00276 } 00277 maybeScrollToBottom(); 00278 } 00279 m_matchCount++; 00280 } 00281 } 00282 00283 00284 void GrepViewWidget::projectChanged(KDevProject *project) 00285 { 00286 QString dir = project? project->projectDirectory() : QDir::homeDirPath(); 00287 grepdlg->setDirectory(dir); 00288 } 00289 00290 #include "grepviewwidget.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:12 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003