findwidget.cpp
author insilmaril
Thu, 18 Mar 2010 11:31:02 +0000
changeset 837 5ecd0462f76b
parent 834 0fad394bc330
permissions -rw-r--r--
Switch to Qt 4.6.0: Removing QtAnimation framework
     1 #include <QAction>
     2 #include <QDebug>
     3 #include <QLineEdit>
     4 #include <QVBoxLayout>
     5 #include <QLabel>
     6 
     7 #include <QComboBox>
     8 #include <QPushButton>
     9 #include <QGroupBox>
    10 #include <QLabel>
    11 
    12 
    13 #include "findwidget.h"
    14 #include "mainwindow.h"
    15 
    16 
    17 extern QString iconPath;
    18 extern Main *mainWindow;
    19 
    20 FindWidget::FindWidget(QWidget *)
    21 {
    22     QVBoxLayout* mainLayout = new QVBoxLayout;
    23     QHBoxLayout *row2Layout = new QHBoxLayout;
    24     
    25 	// Create Buttons
    26 	cancelbutton = new QPushButton;
    27 	//cancelbutton->setText(tr("Cancel"));
    28 	cancelbutton->setIcon (QIcon (iconPath+"fileclose.png"));
    29 	cancelbutton->setShortcut (Qt::Key_Escape);
    30 	connect ( cancelbutton, SIGNAL( clicked() ), this, SLOT( cancelPressed() ) );
    31 	
    32 	QLabel *label=new QLabel;
    33 	label->setText (tr("Find:","FindWidget"));
    34 	
    35 	// Create LineEdit (here QComboBox)
    36     findcombo = new QComboBox;
    37 	findcombo->setMinimumWidth(250);
    38 	findcombo->setEditable(true);
    39 
    40 	QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    41     findcombo->setSizePolicy(sizePolicy);
    42 	connect ( findcombo, SIGNAL( highlighted(int) ), 
    43 		this, SLOT( nextPressed() ) );
    44 	connect ( findcombo, SIGNAL( textChanged(const QString &) ), 
    45 		this, SLOT( findTextChanged(const QString&) ) );
    46 
    47 	nextbutton = new QPushButton;
    48 	nextbutton->setText (tr("Next","Find widget"));
    49 	connect ( nextbutton, SIGNAL( clicked() ), this, SLOT( nextPressed() ) );
    50 
    51 	// QAction needed to only activate shortcut while FindWidget has focus
    52 	QAction *a=new QAction (nextbutton->text(),this);
    53 	a->setShortcut (Qt::Key_Return);
    54 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
    55 	connect ( a, SIGNAL( triggered() ), this, SLOT( nextPressed() ) );
    56 	addAction (a);
    57 
    58 	showAllButton = new QPushButton;
    59 	showAllButton->setText (tr("Show all","Find widget"));
    60 	connect ( showAllButton, SIGNAL( clicked() ), mainWindow, SLOT( editOpenFindWidget() ) );
    61 
    62 	row2Layout->addWidget (cancelbutton);
    63 	row2Layout->addWidget (label);
    64 	row2Layout->addWidget(findcombo);
    65 	row2Layout->addWidget(nextbutton);
    66 	row2Layout->addWidget(showAllButton);
    67 
    68 	mainLayout->addLayout (row2Layout);
    69 
    70 	setLayout (mainLayout);
    71 	status=Undefined;
    72 }
    73 
    74 void FindWidget::popup()
    75 {
    76 	show();
    77 	findcombo->lineEdit()->selectAll();
    78 	findcombo->setFocus();
    79 	setStatus (Undefined);
    80 }
    81 
    82 void FindWidget::cancelPressed()
    83 {
    84 	hide();
    85 	emit (hideFindWidget() );//Restore focus
    86 }
    87 
    88 void FindWidget::nextPressed()
    89 {
    90 	emit (nextButton(findcombo->currentText() ) );
    91 }
    92 
    93 void FindWidget::findTextChanged(const QString&)
    94 {
    95 	setStatus (Undefined);
    96 }
    97 
    98 void FindWidget::setFocus()
    99 {
   100 	findcombo->setFocus();
   101 }
   102 
   103 void FindWidget::setStatus (Status st)
   104 {
   105 	if (st==status) return;
   106 
   107 	status=st;
   108 	QPalette p=palette();
   109 	QColor c;
   110 	switch (st)
   111 	{
   112 		case Success: c=QColor (120,255,120); break;
   113 		case Failed:  c=QColor (255,120,120); break;
   114 		default:  c=QColor (255,255,255); 
   115 	}
   116     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
   117     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
   118     findcombo->setPalette(p);
   119 }
   120