findresultwidget.cpp
author insilmaril
Mon, 14 Jun 2010 13:59:17 +0000
changeset 848 e265f07f2173
parent 842 bec082472471
permissions -rw-r--r--
Fixed tmp relink, colored headings in TreeView
     1 #include "findresultwidget.h"
     2 
     3 #include <QVBoxLayout>
     4 #include <QTreeView>
     5 
     6 #include "findresultitem.h"
     7 #include "findresultmodel.h"
     8 #include "vymmodel.h"
     9 
    10 extern QString iconPath;
    11 
    12 FindResultWidget::FindResultWidget(VymModel *m, QWidget *)
    13 {
    14 	model=m;
    15 
    16 	// Create results model
    17 	resultsModel=new FindResultModel;
    18 
    19 	// Create TreeView
    20 	view = new QTreeView (this);
    21 	view->setModel (resultsModel);
    22 
    23     QVBoxLayout* mainLayout = new QVBoxLayout;
    24     
    25 	// FIXME-4 feature: show number of hits at bottom of FindResultWidget
    26 
    27 	/* FIXME-3 testing QMenuBar *mb=new QMenuBar;
    28 	QAction *a=new  QAction ("Foo action",NULL);
    29 	mb->addAction (a);
    30 	mb->insertSeparator();
    31 	mainLayout->addWidget(mb);
    32 	*/
    33 	mainLayout->addWidget(view);
    34 
    35 	setLayout (mainLayout);
    36 
    37 	model=m;
    38 
    39 	// Selection
    40 	connect (view->selectionModel(),SIGNAL (selectionChanged (QItemSelection,QItemSelection)),
    41 		this, SLOT (updateSelection (QItemSelection,QItemSelection)));
    42 
    43 	connect (resultsModel, SIGNAL(layoutChanged() ), view, SLOT (expandAll() ));	
    44 }
    45 
    46 void FindResultWidget::addItem (TreeItem *ti)
    47 {
    48 	if (ti)
    49 	{
    50 		QModelIndex index = view->selectionModel()->currentIndex();
    51 		//QAbstractItemModel *resultsModel = view->model();
    52 		
    53 		if (!resultsModel->insertRow(index.row()+1, index.parent()))
    54 			return;
    55 
    56 		for (int column = 0; column < resultsModel->columnCount(index.parent()); ++column) {
    57 			QModelIndex child = resultsModel->index(index.row()+1, column, index.parent());
    58 			resultsModel->setData(child, QVariant(ti->getHeading()), Qt::EditRole);
    59 			resultsModel->getItem(child)->setOriginal (ti);
    60 		}
    61 	}
    62 }
    63 
    64 void FindResultWidget::addItem (const QString &s)
    65 {
    66 	if (!s.isEmpty())
    67 	{
    68 		QModelIndex index = view->selectionModel()->currentIndex();
    69 		
    70 		if (!resultsModel->insertRow(index.row()+1, index.parent()))
    71 			return;
    72 
    73 		for (int column = 0; column < resultsModel->columnCount(index.parent()); ++column) {
    74 			QModelIndex child = resultsModel->index(index.row()+1, column, index.parent());
    75 			resultsModel->setData(child, QVariant(s), Qt::EditRole);
    76 		}
    77 	}
    78 }
    79 
    80 void FindResultWidget::setModel (VymModel *m)
    81 {
    82 	if (model !=NULL && m!=model)
    83 		qWarning ("FindResultWidget::setModel  m!=model");
    84 	model=m;	
    85 }
    86 
    87 FindResultModel* FindResultWidget::getResultModel()
    88 {
    89 	return resultsModel;
    90 }
    91 
    92 void FindResultWidget::popup()
    93 {
    94 	show();
    95 	parentWidget()->show();
    96 }
    97 
    98 void FindResultWidget::cancelPressed()
    99 {
   100 	emit (hideFindResultWidget() );
   101 }
   102 
   103 void FindResultWidget::updateSelection(QItemSelection newsel,QItemSelection)
   104 {
   105 	QModelIndex ix;
   106 	foreach (ix,newsel.indexes() )
   107 	{
   108 		FindResultItem *fri= static_cast<FindResultItem*>(ix.internalPointer());
   109 		if (fri->getOrgModel() && fri->getOriginalID()>0)
   110 		{
   111 			TreeItem *ti=fri->getOrgModel()->findID(fri->getOriginalID() );
   112 			if (ti)
   113 			{
   114 				fri->getOrgModel()->select (ti);
   115 				int i=fri->getOriginalIndex();
   116 				if (i>=0) emit (noteSelected (resultsModel->getSearchString(),i));
   117 			}	
   118 		}
   119 	}
   120 }
   121 
   122