#!/usr/local/bin/ruby
require 'classtree.rb'
require 'qt2'
include Qt2

#GC.disable
class RClassMain < QMainWindow
def initialize
   @w0 = QWidget.new(self)
   @splitter = QSplitter.new(Qt::Vertical,@w0,"split1")
   @popup = QPopupMenu.new
   @popup.insertItem("&Qt2",1)
   @popup.insertItem("&Gtk",2)
   @popup.insertItem("&Fltk",3)
   @popup.insertItem("&Tk",4)
   @popup.insertItem("&All",5)
   #@app = QApplication::qApp
   @popup.insertItem( "&Quit", QApplication::qApp, "quit()", CTRL+Key_Q )
   menuBar.insertItem("&File",@popup)
   @classWindow = RClassView.new(@splitter)
   Qt2::connect(menuBar,QSIGNAL("activated(int)"), self, "menuActivated")

   qsl = QStringList.new
   @home = `echo $QTDIR`.chop! + "/doc/html/index.html"
   qsl.append(".")
   @edit = QTextBrowser.new(@splitter)
   @edit.mimeSourceFactory().setFilePath( qsl )
   @edit.setSource(@home)

   setCentralWidget(@w0)
   Qt2::connect(@classWindow.signal_class_selected,self,"classSelected")
   Qt2::connect(@classWindow.signal_mlist,self,"methodSelected")
   Qt2::connect(@classWindow.signal_vlist,self,"constSelected")

   catchEvent
   setCaption("Ruby Class Browser")
   statusBar.message( "Ready", 5000 )
end
def menuActivated i
   case i
   when 1
      @classWindow.initialize_classview_qt
   when 2
      @classWindow.initialize_classview_gtk
   when 3
      @classWindow.initialize_classview_fltk
   when 4
      @classWindow.initialize_classview_tk
   when 5
      @classWindow.initialize_classview_Object
   end
end
def classSelected cname
   class_file = cname.downcase
   class_file.gsub!(/q.*::/,"")
   class_html = `echo $QTDIR`.chop! + "/doc/html/#{class_file}.html"
   if (File::exist?(class_html))
      @edit.setSource(class_html)
   end
end
def methodSelected str
   if str[0].chr == "-"
      #@edit.clear
      @edit.setText("No method selected")
      return
   end
   str1 = "Selected Method is "
   str1 += str + " "
   str1 += " in " + "#{@classWindow.currentClass}"
   #@edit.clear
   @edit.setText(str1)
end
def constSelected str
   str1 = str + " in " +  "#{@classWindow.currentClass} = "
   temp1 = eval("#{@classWindow.currentClass}::#{str}")
   str1 += "#{temp1}"
   #@edit.clear
   @edit.setText(str1)
end
def resizeEvent ev
      @splitter.resize(width,height)
end
end

class RClassView < QWidget
attr_reader :signal_class_selected, :signal_mlist, :signal_vlist
def initialize w
   @split = QSplitter.new(Qt::Horizontal,self,"split2")
   @classview = QListView.new(@split,"classview")
   @classview.addColumn("Ruby Class")
   @classview.setColumnWidthMode(0,QListView::Maximum)
   @classview.setRootIsDecorated( true )

   @w1 = QWidget.new(@split)
   @hblayout = QHBoxLayout.new(@w1)
   @vblayout1 = QVBoxLayout.new(@hblayout)

   @mlisthead = QLabel.new("Methods",@w1,"mlisthead")
   @mlisthead.setFrameStyle(QFrame::Panel | QFrame::Raised)
   @vblayout1.addWidget(@mlisthead)
   @methodlist = QListBox.new(@w1,"methodlist")
   @vblayout1.addWidget(@methodlist)

   @signal_class_selected = RSignal.new("Object")

   @signal_mlist = RSignal.new("const QString &")
   Qt2::connect(@methodlist,QSIGNAL("selected(const QString&)"),
             @signal_mlist)

   @vblayout2 = QVBoxLayout.new(@hblayout)
   @varlisthead = QLabel.new("Constants",@w1,"varlisthead")
   @varlisthead.setFrameStyle(QFrame::Panel | QFrame::Raised)
   @vblayout2.addWidget(@varlisthead)
   @varlist = QListBox.new(@w1,"varlist")
   @vblayout2.addWidget(@varlist)
   @signal_vlist = RSignal.new("const QString &")
   Qt2::connect(@varlist,QSIGNAL("selected(const QString&)"),
            @signal_vlist)


   newheight = @classview.header.height - 10
   @mlisthead.setFixedHeight(newheight)
   @varlisthead.setFixedHeight(newheight)


   @hblayout.activate

   initialize_classview_qt
   Qt2::connect(@classview,QSIGNAL("selectionChanged()"),self,"cb_select")

   catchEvent


end
def resizeEvent ev
   @split.resize(width,height)
end

def currentClass
   @classview.currentItem.text(0).to_str
end
def currentMethod
   @methodlist.text(@methodlist.currentItem)
end
def currentConst
   @varlist.text(@varlist.currentItem)
end
def resizeEvent ev
   @split.resize(width,height)
end

def initialize_classview_qt
   @classview.clear
   @classtree = ClassTree.new(["Qt2"])
   @classtree.createTree(@classview)
#   @classtree.createConstantslist(@varlist)
#   @classtree.createMethodsList(@methodlist)
end

def initialize_classview_gtk
   require "gtk"
   @classview.clear
   @classtree = ClassTree.new(["Gdk","Gtk"])
   @classtree.createTree(@classview)
end
def initialize_classview_fltk
   require "fltk"
   @classview.clear
   @classtree = ClassTree.new(["Fltk"])
   @classtree.createTree(@classview)
end
def initialize_classview_tk
   require "tk"
   @classview.clear
   @classtree = ClassTree.new(["Tk","TkKernel"])
   @classtree.createTree(@classview)
end
def initialize_classview_Object
   @classview.clear
   @classtree = ClassTree.new(["Object"])
   @classtree.createTree(@classview)
end
def cb_select
   cname = @classview.currentItem.text(0).to_str
      @signal_class_selected.sendWith(cname)
      @classtree.changeConstants(@varlist,cname)
      @classtree.changeMethods(@methodlist,cname)
end
end


a = QApplication.new( [$0] + ARGV)
style = Qt2::QMotifStyle.new
QApplication::setStyle(style)
w = RClassMain.new
a.setMainWidget(w)
w.resize(640,400)
w.show
GC.start
a.exec
