~henning-schroeder/+junk/codeaide

« back to all changes in this revision

Viewing changes to codeaide/ide/base.py

  • Committer: Henning Schröder
  • Date: 2009-03-16 11:27:51 UTC
  • Revision ID: henning.schroeder@gmail.com-20090316112751-t3fa6y8xhmrapy51
added ide sub-package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.5
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
import sys
 
5
import atexit
 
6
import os
 
7
from PyQt4.QtCore import (
 
8
    Qt, SIGNAL,
 
9
    QTimer)
 
10
from PyQt4.QtGui import (
 
11
    QApplication, qApp, QSplashScreen,
 
12
    QMainWindow, QDockWidget, QMenu, QToolBar, QAction,
 
13
    QIcon, QKeySequence,
 
14
    QHBoxLayout,
 
15
    QWidget, QTabWidget,
 
16
    QFileDialog)
 
17
 
 
18
 
 
19
from codeaide.interface import implements
 
20
import api
 
21
 
 
22
 
 
23
class i18n(unicode):
 
24
    pass
 
25
 
 
26
 
 
27
 
 
28
 
 
29
def call_on_idle(func):
 
30
    QTimer.singleShot(0, func)
 
31
 
 
32
 
 
33
 
 
34
 
 
35
 
 
36
class ToolbarManager(object):
 
37
 
 
38
 
 
39
    def __init__(self, mainwin):
 
40
        self.mainwin = mainwin
 
41
        self.toolbars = {}
 
42
 
 
43
 
 
44
    def get_toolbar(self, title):
 
45
        tb = self.toolbars.get(title)
 
46
        return tb
 
47
 
 
48
 
 
49
    def add_toolbar(self, title):
 
50
        tb = QToolBar(title, self.mainwin)
 
51
        group = self.mainwin.addToolBar(tb)
 
52
        self.toolbars[title] = tb
 
53
        return tb
 
54
 
 
55
 
 
56
    def get_or_create_toolbar(self, title):
 
57
        tb = self.get_toolbar(title)
 
58
        if not tb:
 
59
            tb = self.add_toolbar(title)
 
60
        return tb
 
61
 
 
62
 
 
63
    def add_toolbar_action(self, tb_title, action):
 
64
        tb = self.get_or_create_toolbar(tb_title)
 
65
        tb.addAction(action)
 
66
 
 
67
 
 
68
 
 
69
class MenuManager(object):
 
70
 
 
71
 
 
72
    def __init__(self, menubar):
 
73
        self.menubar = menubar
 
74
        self.menus = {}
 
75
 
 
76
 
 
77
    def get_menu(self, title):
 
78
        menu = self.menus.get(title)
 
79
        return menu
 
80
 
 
81
 
 
82
    def add_menu(self, title):
 
83
        menu = QMenu(title, self.menubar)
 
84
        self.menubar.addMenu(menu)
 
85
        self.menus[title] = menu
 
86
        return menu
 
87
 
 
88
 
 
89
    def get_or_create_menu(self, title):
 
90
        menu = self.get_menu(title)
 
91
        if not menu:
 
92
            menu = self.add_menu(title)
 
93
        return menu
 
94
 
 
95
 
 
96
    def add_menu_action(self, menu_title, item_action):
 
97
        menu = self.get_or_create_menu(menu_title)
 
98
        menu.addAction(item_action)
 
99
 
 
100
 
 
101
 
 
102
 
 
103
 
 
104
class CommandBase(object):
 
105
 
 
106
    title = None
 
107
    menu = None
 
108
    shortcut = None
 
109
    icon = None
 
110
    toolbar = None
 
111
 
 
112
 
 
113
    def __init__(self, parent):
 
114
        self.action = QAction(parent)
 
115
        if self.title:
 
116
            self.action.setText(self.title)
 
117
        if self.icon:
 
118
            self.action.setIcon(QIcon(self.icon))
 
119
        if self.shortcut:
 
120
            self.action.setShortcut(QKeySequence(self.shortcut))
 
121
        if self.menu:
 
122
            parent.menu_manager.add_menu_action(self.menu, self.action)
 
123
        self.action.connect(self.action, SIGNAL("triggered()"), self.execute)
 
124
        if self.toolbar:
 
125
            parent.toolbar_manager.add_toolbar_action(self.toolbar, self.action)
 
126
        self.parent = parent
 
127
 
 
128
 
 
129
    def execute(self):
 
130
        raise NotImplementedError, self.execute
 
131
 
 
132
 
 
133
 
 
134
class CheckCommandBase(CommandBase):
 
135
 
 
136
    checked = False
 
137
 
 
138
    def __init__(self, parent):
 
139
        CommandBase.__init__(self, parent)
 
140
        self.action.setCheckable(True)
 
141
        self.action.setChecked(self.checked)
 
142
 
 
143
 
 
144
 
 
145
 
 
146
class DockBase(object):
 
147
 
 
148
    title = None
 
149
    area = Qt.BottomDockWidgetArea
 
150
    toggle_menu = None
 
151
    toggle_shortcut = None
 
152
    icon = None
 
153
 
 
154
 
 
155
    def __init__(self, parent):
 
156
        self.parent = parent
 
157
        call_on_idle(self._setup_ui)
 
158
 
 
159
 
 
160
    def _setup_ui(self):
 
161
        self.dock = QDockWidget(self.parent)
 
162
        self.dock.setWindowTitle(self.title)
 
163
        self.widget = self.create_widget(self.dock)
 
164
        self.dock.setWidget(self.widget)
 
165
        self.parent.addDockWidget(self.area, self.dock)
 
166
 
 
167
        if self.toggle_menu:
 
168
            action = self.dock.toggleViewAction()
 
169
            if self.icon:
 
170
                action.setIcon(QIcon(self.icon))
 
171
            if self.toggle_shortcut:
 
172
                action.setShortcut(QKeySequence(self.toggle_shortcut))
 
173
            self.parent.menu_manager.add_menu_action(self.toggle_menu, action)
 
174
 
 
175
 
 
176
    def create_widget(self, parent):
 
177
        raise NotImplementedError, self.create_widget