]> git.sven.stormbind.net Git - sven/vym.git/blob - vymview.cpp
fb2c80ec0dcec142ec4cbdc1d603de3808330f39
[sven/vym.git] / vymview.cpp
1 #include "vymview.h"
2
3 #include "branchitem.h"
4 #include "dockeditor.h"
5 #include "mainwindow.h"
6 #include "mapeditor.h"
7 #include "treedelegate.h"
8 #include "slideeditor.h"
9 #include "treeeditor.h"
10
11 extern Main *mainWindow;
12 extern Settings settings;
13
14 VymView::VymView(VymModel *m)
15 {
16     model=m;
17     model->setView (this);
18
19     // Create TreeView
20     treeEditor=new TreeEditor (model);
21
22     selModel=new QItemSelectionModel (model);
23     model->setSelectionModel (selModel);
24
25     treeEditor->setSelectionModel (selModel);
26     treeEditor->setColumnWidth (0,150);
27     treeEditor->setAnimated (true);
28     treeEditor->resize ( 20,200);
29
30     TreeDelegate *delegate=new TreeDelegate (this);
31     treeEditor->setItemDelegate (delegate);
32
33     DockEditor *de;
34     de = new DockEditor (tr("Tree Editor","Title of dockable editor widget"), this, model);
35     de->setWidget (treeEditor);
36     de->setAllowedAreas (Qt::AllDockWidgetAreas);
37     addDockWidget(Qt::LeftDockWidgetArea, de);
38     treeEditorDE=de;
39
40     connect (
41         treeEditorDE, SIGNAL (visibilityChanged(bool) ), 
42         mainWindow,SLOT (updateActions() ) );
43
44     // Create good old MapEditor
45     mapEditor=model->getMapEditor();
46     if (!mapEditor) mapEditor=new MapEditor (model);
47     setCentralWidget (mapEditor);
48
49     // Create SlideEditor
50     slideEditor=new SlideEditor (model);
51
52     de = new DockEditor (tr("Slide Editor","Title of dockable editor widget"), this, model);
53     de->setWidget (slideEditor);
54     de->setAllowedAreas (Qt::AllDockWidgetAreas);
55     addDockWidget(Qt::RightDockWidgetArea, de);
56     slideEditorDE=de;
57     slideEditorDE->hide();
58     connect (
59         slideEditorDE, SIGNAL (visibilityChanged(bool) ), 
60         mainWindow,SLOT (updateActions() ) );
61
62     // Create Layout 
63     /*
64     QVBoxLayout* mainLayout = new QVBoxLayout (this); 
65     QSplitter *splitter= new QSplitter (this);
66
67     QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68     splitter->setSizePolicy(sizePolicy);
69     mainLayout->addWidget (splitter);
70     */
71
72     // Connect selections
73
74         // Selection in Model changed   
75         connect (
76             selModel, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
77             this,SLOT (changeSelection(const QItemSelection &,const QItemSelection &)));
78
79         // Needed to update selbox during animation 
80         connect (
81             model, SIGNAL (selectionChanged(const QItemSelection &, const QItemSelection &)), 
82             mapEditor,SLOT (updateSelection(const QItemSelection &,const QItemSelection &)));
83
84     // Connect data changed signals 
85     connect (
86         model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
87         mapEditor,SLOT (updateData(const QModelIndex &) ) );
88
89     connect (
90         model, SIGNAL (dataChanged(const QModelIndex &, const QModelIndex &)), 
91         this, SLOT (updateDockWidgetTitles() ) );
92
93     connect (
94         model, SIGNAL (updateQueries (VymModel*)), 
95         mainWindow,SLOT (updateQueries(VymModel*) ) );
96
97     connect (
98         model, SIGNAL (noteChanged(QModelIndex) ),
99         mainWindow, SLOT (updateNoteEditor (QModelIndex) ) );
100         
101     connect (
102         model, SIGNAL (expandAll() ),
103         this, SLOT (expandAll () ) );
104         
105     connect (
106         model, SIGNAL (expandOneLevel() ),
107         this, SLOT (expandOneLevel() ) );
108         
109     connect (
110         model, SIGNAL (collapseOneLevel() ),
111         this, SLOT (collapseOneLevel() ) );
112         
113     connect (
114         model, SIGNAL (collapseUnselected() ),
115         this, SLOT (collapseUnselected() ) );
116         
117     connect (
118         model, SIGNAL (showSelection() ),
119         this, SLOT (showSelection() ) );
120         
121     connect (
122         model, SIGNAL (updateLayout() ),
123         mapEditor, SLOT (autoLayout() ) );
124         
125     mapEditor->setAntiAlias (mainWindow->isAliased());
126     mapEditor->setSmoothPixmap(mainWindow->hasSmoothPixmapTransform());
127 }
128
129 VymView::~VymView() 
130 {
131     if (treeEditorIsVisible() )
132         settings.setLocalValue(model->getFilePath(),"/treeeditor/visible","true");
133     else
134         settings.setLocalValue(model->getFilePath(),"/treeeditor/visible","false");
135 }
136
137 void VymView::readSettings()
138 {
139     if (settings.localValue(model->getFilePath(),"/slideeditor/visible","false").toBool() )
140         slideEditorDE->show();
141     else
142         slideEditorDE->hide();
143     if (settings.localValue(model->getFilePath(),"/treeeditor/visible","true").toBool() )
144         treeEditorDE->show();
145     else
146         treeEditorDE->hide();
147 }
148
149 VymModel* VymView::getModel()
150 {
151     return model;
152 }
153
154 MapEditor* VymView::getMapEditor()
155 {
156     return mapEditor;
157 }
158
159 bool VymView::treeEditorIsVisible()
160 {
161     return treeEditorDE->isVisible();
162 }
163
164 bool VymView::slideEditorIsVisible()
165 {
166     return slideEditorDE->isVisible();
167 }
168
169 void VymView::initFocus()
170 {
171     mapEditor->setFocus();
172 }
173
174 void VymView::nextSlide()
175 {
176     slideEditor->nextSlide();
177 }
178
179 void VymView::previousSlide()
180 {
181     slideEditor->previousSlide();
182 }
183
184 void VymView::changeSelection (const QItemSelection &newsel, const QItemSelection &desel)  
185 {
186     // Update note editor and heading editor // FIXME-3 improve this, evtl. move from mainwindow to here
187     model->updateSelection (newsel,desel);
188     mainWindow->changeSelection (model,newsel,desel);   
189     mainWindow->updateDockWidgetTitles( model );
190     mapEditor->updateSelection (newsel,desel);
191     showSelection();
192 }
193
194 void VymView::updateDockWidgetTitles()
195 {
196     mainWindow->updateDockWidgetTitles( model );
197 }
198
199 void VymView::expandAll()
200 {
201     treeEditor->expandAll();
202 }
203
204 void VymView::expandOneLevel()
205 {
206     int level=999999;
207     int d;
208     BranchItem *cur=NULL;
209     BranchItem *prev=NULL;
210     QModelIndex pix;
211
212     // Find level to expand
213     model->nextBranch(cur,prev);
214     while (cur) 
215     {
216         pix=model->index (cur);
217         d=cur->depth();
218         if (!treeEditor->isExpanded(pix) && d < level)
219             level=d;
220         model->nextBranch(cur,prev);    
221     }
222
223     // Expand all to level
224     cur=NULL;
225     prev=NULL;
226     model->nextBranch(cur,prev);
227     while (cur) 
228     {
229         pix=model->index (cur);
230         d=cur->depth();
231         if (!treeEditor->isExpanded(pix) && d <= level && cur->branchCount()>0)
232             treeEditor->setExpanded(pix,true);
233         model->nextBranch(cur,prev);    
234     }
235 }
236
237 void VymView::collapseOneLevel()
238 {
239     int level=-1;
240     int d;
241     BranchItem *cur=NULL;
242     BranchItem *prev=NULL;
243     QModelIndex pix;
244
245     // Find level to collapse
246     model->nextBranch(cur,prev);
247     while (cur) 
248     {
249         pix=model->index (cur);
250         d=cur->depth();
251         if (treeEditor->isExpanded(pix) && d > level)
252             level=d;
253         model->nextBranch(cur,prev);    
254     }
255
256     // collapse all to level
257     cur=NULL;
258     prev=NULL;
259     model->nextBranch(cur,prev);
260     while (cur) 
261     {
262         pix=model->index (cur);
263         d=cur->depth();
264         if (treeEditor->isExpanded(pix) && d >= level)
265             treeEditor->setExpanded(pix,false);
266         model->nextBranch(cur,prev);    
267     }
268 }
269
270 void VymView::collapseUnselected()
271 {
272     BranchItem *cur=NULL;
273     BranchItem *prev=NULL;
274     QModelIndex pix;
275
276     // Find level to collapse
277     TreeItem *selti=model->getSelectedItem();
278     if (!selti) return;
279
280     int level=selti->depth();
281
282     // collapse all to level
283     model->nextBranch(cur,prev);
284     //bool b=false;
285     while (cur) 
286     {
287         pix=model->index (cur);
288         if (treeEditor->isExpanded(pix) &&  level <= cur->depth())
289         {
290             treeEditor->setExpanded(pix,false);
291             //b=true;
292         }
293         model->nextBranch(cur,prev);    
294     }
295
296 /* FIXME-3 "collapse more" unimplemented yet
297     if (b) return;
298
299     // If we didn't collapse anything so far collapse more
300     qDebug()<<"VM::collapse more";
301     cur=NULL;
302     prev=NULL;
303
304     // Find level to collapse
305     model->nextBranch(cur,prev);
306     while (cur) 
307     {
308         pix=model->index (cur);
309         d=cur->depth();
310         if (treeEditor->isExpanded(pix) && d > level && )
311             level=d;
312         model->nextBranch(cur,prev);    
313     }
314
315     // collapse all to level
316     model->nextBranch(cur,prev);
317     bool b=false;
318     while (cur) 
319     {
320         pix=model->index (cur);
321         if (treeEditor->isExpanded(pix) &&  level <= cur->depth())
322         {
323             treeEditor->setExpanded(pix,false);
324             b=true;
325         }
326         model->nextBranch(cur,prev);    
327     }
328 */
329 }
330
331 void VymView::showSelection()
332 {
333     QModelIndex ix=model->getSelectedIndex();
334     treeEditor->scrollTo( ix, QAbstractItemView::EnsureVisible);
335     mapEditor->scrollTo ( ix);  
336 }
337
338 void VymView::toggleTreeEditor()
339 {
340     if (treeEditorDE->isVisible() )
341     {
342         treeEditorDE->hide();
343         settings.setLocalValue(model->getFilePath(),"/treeeditor/visible","false");
344     } else
345     {
346         treeEditorDE->show();
347         settings.setLocalValue(model->getFilePath(),"/treeeditor/visible","true");
348     }   
349     model->setChanged();
350 }
351
352 void VymView::toggleSlideEditor()
353 {
354     if (slideEditorDE->isVisible() )
355     {
356         slideEditorDE->hide();
357         settings.setLocalValue(model->getFilePath(),"/slideeditor/visible","false");
358     } else
359     {
360         slideEditorDE->show();
361         settings.setLocalValue(model->getFilePath(),"/slideeditor/visible","true");
362     }
363 }
364
365 void VymView::setFocusMapEditor()
366 {
367     mapEditor->setFocus();
368 }