summaryrefslogtreecommitdiffstats
path: root/korundum/rubylib/examples/uitdemdi.rb
blob: fe5655c69e4d573a639fae984d181efb6654e379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

=begin
This is a ruby version of Jim Bublitz's python original. The ruby behaviour
is 'crash for crash' identical - so the the problems described below are 
related to KMDI, and not the bindings.
=end

=begin
A rough Python translation of the ideas presented in this KMDI tutorial:

    http://web.tiscali.it/andreabergia/tdemditutorial.html

What does work:

    IDEAlMode - yay!

    Adding and closing child views

    Two-way syncing between a tool widget and a matching child view

All is not rosy, however:

    Instances of the KmdiExample maintain a dictionary of child views.  Values
    cannot be deleted from this dictionary during a window close (causes an
    immediate segfault).

    Child views created after initialization aren't numbered correctly; given
    the first problem, it's harder to do this than it's really worth.

    The example segfaults at shutdown if the tool (on the left) is is open but
    is not in overlap-mode.

=end

require 'Korundum'
include KDE

class KmdiExample < KDE::MdiMainFrm

	slots	'closeChild(KMdiChildView*)',
			'syncFromChildView(KMdiChildView*)',
			'syncFromMainTool(TQListBoxItem*)',
			'activatedMessage(KMdiChildView*)',
			'newView()', 'close()', 'closeActiveChild()'
			
	def getIcon(name, group=Icon::NoGroup, size=Icon::SizeSmall)
    	# returns a kde icon by name
    	return Global.instance().iconLoader().loadIcon(name, group, size)
	end

    def initialize(parent=nil)
        super(parent, 'KmdiExample', Mdi::IDEAlMode)

    	@viewIcons = ['network', 'email', 'stop', 'back', 'forward']
    	@toolIcons = ['view_icon', 'configure']
	
        openNewAction = StdAction.openNew(self, TQ_SLOT('newView()'), actionCollection())
        quitAction = StdAction.quit(self, TQ_SLOT('close()'), actionCollection())
        closeAction = StdAction.close(self, TQ_SLOT('closeActiveChild()'), actionCollection())
    	
		uifilebase = Dir.getwd + '/uitdemdi.rc'
        createGUI(uifilebase)
		# The task bar is created in the KMdiMainFrm constructor
		# and then deleted in the createGUI() call above..
		# So recreate it again to avoid a crash.
		createTaskBar() 
        statusBar()
        resize(400, 300)

        @tools = {}
		@toolIcons.each_index do |idx|
			ico = @toolIcons[idx]
			wid = KDE::ListBox.new(self, "list#{idx.to_s}")
			makeTool(wid, "Tool #{idx.to_s}", ico)
		end
        ## smells
        @mainToolWidget = @maintool = @tools['Tool 0'][0]

        @childs = {}
		@viewIcons.each_index do |idx|
			ico = @viewIcons[idx]
            makeView("View #{idx.to_s}", ico, ico)
		end

        connect(self, TQ_SIGNAL('viewActivated(KMdiChildView*)'), self, TQ_SLOT('activatedMessage(KMdiChildView*)'))
        connect(self, TQ_SIGNAL('viewActivated(KMdiChildView*)'), self, TQ_SLOT('syncFromChildView(KMdiChildView*)'))
        connect(@maintool, TQ_SIGNAL('selectionChanged(TQListBoxItem*)'), self, TQ_SLOT('syncFromMainTool(TQListBoxItem*)'))
        syncFromChildView(activeWindow())
	end

    def syncFromMainTool(item)
        # activate the view that matches the item text
         activateView(findWindow(item.text()))
	end

    def syncFromChildView(child)
        # sync the main tool to the indicated child
       @maintool = @mainToolWidget
		if child.nil?
			return
		end
        item = @maintool.findItem(child.tabCaption())
        if !item.nil?
            @maintool.setSelected(item, nil)
		end
	end

    def makeTool(widget, caption, icon, percent=50)
        # makes a tool from the widget
        tip = i18n("#{caption} Tool Tip")
        dock = DockWidget::DockLeft
        maindock = getMainDockWidget()
        widget.setIcon(getIcon(icon))
        tool = addToolWindow(widget, dock, maindock, percent, tip, caption)
        @tools[caption] = [widget, tool]
	end

    def makeView(label, icon, text)
        # makes a child view with a text label and a pixmap label
        view = MdiChildView.new(label, self)
        @childs[label] = view
        view.setIcon(getIcon(icon))
        layout = TQt::VBoxLayout.new(view)
        layout.setAutoAdd(true)

        lbl = TQt::Label.new(i18n("Label for a view with an icon named #{text}"), view)
        pxm = TQt::Label.new('', view)
        pxm.setPixmap(getIcon(icon, Icon::NoGroup, KDE::Icon::SizeLarge))
        addWindow(view)
        @mainToolWidget.insertItem(label)
        connect(view, TQ_SIGNAL('childWindowCloseRequest(KMdiChildView*)'), self, TQ_SLOT('closeChild(KMdiChildView*)'))
	end

    def removeMainToolItem(view)
        # remove item from the main list tool that corresponds to the view
        @maintool = @mainToolWidget
        @maintool.takeItem(@maintool.findItem(view.tabCaption(), 0))
	end

    def newView()
        # make a view when the user invokes the new action
        makeView("View ", 'network', 'A Fresh View')
#        makeView("View #{@childs.length}", 'network', 'A Fresh View')
        syncFromChildView(activeWindow())
	end

    def closeActiveChild()
        # close the current view
        removeMainToolItem(activeWindow())
        closeActiveView()
        syncFromChildView(activeWindow())
	end

    def closeChild(which)
        # called to close a view from its tab close button
        caption = which.tabCaption()
        removeMainToolItem(which)
        which.close()
        statusBar().message(i18n("#{caption} closed"))
        syncFromChildView(activeWindow())
	end

    def activatedMessage(view)
        # updates the status bar with the caption of the current view
        statusBar().message(i18n("#{view.tabCaption()} activated"))
	end
end

if $0 == __FILE__
    aname = 'uitdemdi'
    desc = 'A Simple Korundum KMDI Sample'
    ver = '1.0'
    lic = AboutData::License_GPL
    author = 'Troy Melhase'
    authormail = 'troy@gci.net'

    about = AboutData.new(aname, aname, ver, desc, lic, "#{authormail} (c) 2004")
    about.addAuthor(author, 'hi, mom!', authormail)
    about.addAuthor('Jim Bublitz', 'For PyKDE', 'jbublitz@nwinternet.com')
    about.addAuthor('Richard Dale', 'For Korundum', 'Richard_Dale@tipitina.demon.co.uk')
    CmdLineArgs.init(ARGV, about)
    app = KDE::Application.new
    mainWindow = KmdiExample.new
    mainWindow.show
    app.exec
end