File indexing completed on 2024-12-22 04:17:30

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "editmultiplewidget.h"
0014 
0015 #include <QRegExp>
0016 #include <QDebug>
0017 
0018 namespace Kst {
0019 
0020 EditMultipleWidget::EditMultipleWidget(QWidget *parent)
0021   : QWidget(parent) {
0022   setupUi(this);
0023 
0024   connect(_selectAllBut, SIGNAL(clicked()), this, SLOT(selectAllObjects()));
0025 
0026   connect(_selectNoneBut, SIGNAL(clicked()), _objectList, SLOT(clearSelection()));
0027 
0028   connect(_filterEdit, SIGNAL(textChanged(QString)), this, SLOT(applyFilter(QString)));
0029   connect(_objectList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SIGNAL(itemDoubleClicked()));
0030 }
0031 
0032 
0033 EditMultipleWidget::~EditMultipleWidget() {}
0034 
0035 
0036 void EditMultipleWidget::selectAllObjects() {
0037   _objectList->selectAll();
0038 }
0039 
0040 
0041 void EditMultipleWidget::applyFilter(const QString& filter) {
0042   _objectList->clearSelection();
0043 
0044   // case insensitive and wildcards
0045   QRegExp re('*' + filter + '*', Qt::CaseInsensitive, QRegExp::Wildcard);
0046 
0047   uint c = _objectList->count();
0048   for (uint i = 0; i < c; ++i) {
0049     if (re.exactMatch(_objectList->item(i)->text())) {
0050       _objectList->item(i)->setSelected(true);
0051     }
0052   }
0053 }
0054 
0055 
0056 // void EditMultipleWidget::addObjects(QStringList &objects) {
0057 //   _objectList->clear();
0058 //   _objectList->addItems(objects);
0059 // }
0060 
0061 
0062 void EditMultipleWidget::clearObjects() {
0063   _objectList->clear();
0064 }
0065 
0066 void EditMultipleWidget::addObject(QString name, QString descriptionTip) {
0067   QListWidgetItem *wi = new QListWidgetItem(name);
0068   wi->setToolTip(descriptionTip);
0069   _objectList->addItem(wi);
0070 }
0071 
0072 QStringList EditMultipleWidget::selectedObjects() const{
0073   QStringList objects;
0074   QList<QListWidgetItem *> selectedItems = _objectList->selectedItems();
0075   foreach (QListWidgetItem *item, selectedItems) {
0076     objects.append(item->text());
0077   }
0078   return objects;
0079 }
0080 
0081 void EditMultipleWidget::selectObjects(const QStringList &objects) {
0082   int n = _objectList->count();
0083   for (int i=0; i<n; i++) {
0084     QListWidgetItem *item = _objectList->item(i);
0085     if (item) {
0086       if (objects.contains(item->text())) {
0087         item->setSelected(true);
0088       }
0089     }
0090   }
0091 }
0092 
0093 void EditMultipleWidget::setHeader(const QString &header)
0094 {
0095   _header->setText(header);
0096 }
0097 
0098 }
0099 
0100 // vim: ts=2 sw=2 et