scide: DocManager - report warnings via the status bar
[supercollider.git] / editors / sc-ide / widgets / doc_list.cpp
blobd62b8d36d58dc5bf85c972094ec79bc5e3b9a7d5
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "doc_list.hpp"
22 #include "../core/doc_manager.hpp"
24 #include <QApplication>
25 #include <QStyle>
27 namespace ScIDE {
29 DocumentListWidget::DocumentListWidget(DocumentManager *manager, QWidget * parent):
30 QListWidget(parent),
31 mDocModifiedIcon( QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton) )
33 connect(manager, SIGNAL(opened(Document*, int, int)), this, SLOT(onOpen(Document*, int, int)));
34 connect(manager, SIGNAL(closed(Document*)), this, SLOT(onClose(Document*)));
35 connect(manager, SIGNAL(saved(Document*)), this, SLOT(onSaved(Document*)));
36 connect(&mModificationMapper, SIGNAL(mapped(QObject*)),
37 this, SLOT(onModificationChanged(QObject*)));
38 connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
39 this, SLOT(onItemClicked(QListWidgetItem*)));
42 void DocumentListWidget::setCurrent( Document *doc )
44 if(!doc)
45 setCurrentRow(-1);
46 else {
47 Item *itm = itemFor(doc);
48 if(itm)
49 setCurrentItem(itm);
53 void DocumentListWidget::onOpen( Document *doc, int, int )
55 addItemFor(doc);
58 void DocumentListWidget::onClose( Document *doc )
60 delete itemFor(doc);
63 void DocumentListWidget::onSaved( Document *doc )
65 Item *item = itemFor(doc);
66 if(item)
67 item->setText(doc->title());
70 void DocumentListWidget::onModificationChanged( QObject * obj )
72 Document *doc = qobject_cast<Document*>(obj);
73 Item *item = itemFor(doc);
74 if(item)
75 item->setIcon(
76 doc->textDocument()->isModified() ?
77 mDocModifiedIcon : QIcon()
81 void DocumentListWidget::onItemClicked(QListWidgetItem* litem)
83 Item *item = itemFor(litem);
84 if(item)
85 Q_EMIT( clicked(item->mDoc) );
88 DocumentListWidget::Item * DocumentListWidget::addItemFor( Document *doc )
90 Item *item = new Item(doc, this);
92 QTextDocument *tdoc = doc->textDocument();
94 if(tdoc->isModified())
95 item->setIcon( mDocModifiedIcon );
97 mModificationMapper.setMapping(tdoc, doc);
98 connect(tdoc, SIGNAL(modificationChanged(bool)),
99 &mModificationMapper, SLOT(map()));
101 return item;
104 DocumentListWidget::Item *DocumentListWidget::itemFor( Document *doc )
106 int c = count();
107 for(int i = 0; i < c; ++i)
109 Item *itm = itemFor( item(i) );
110 if(itm && itm->mDoc == doc) return itm;
112 return 0;
115 DocumentListWidget::Item *DocumentListWidget::itemFor( QListWidgetItem *litem )
117 if(litem->type() == QListWidgetItem::UserType)
118 return static_cast<Item*>(litem);
119 else
120 return 0;
123 } // namespace ScIDE