require "qt2.so"
include Qt2
#Qt2 module functions - ruby part

module Qt2
#Translate Qt2 signal and slot to argument of connect function
def QSIGNAL(str)
	"2" + str
end

def QSLOT(str)
	"1" + str
end

def QMAX(a,b)
  if(a > b)
    return a 
  else
    return b 
  end
end

def QMIN(a,b)
  if(a > b)
    return b
  else
    return a
  end
end

def tr(str)
    QObject::tr(str)
end

#quick hack for QString and QCString sprintf() method
class QString
def sprintf(*arg)
   s = Kernel.sprintf(*arg)
   self.append(QString::fromLocal8Bit(s))
end
end

class QCString
def sprintf(*arg)
   s = Kernel.sprintf(*arg)
   self.append(s)
end
end
class << QApplication
   alias qApp_qt2 qApp
end
def QApplication.qApp
   if(@qApp.nil?)
      @qApp = QApplication.qApp_qt2
   end
   @qApp
end
#Modifier of installEventFilter function
#Modified connect command
class QObject
def catchEvent
   @eventfilter = QR2EventFilter.new(self)
   self.installEventFilter(@eventfilter)
end
def connect(arg0,arg1,arg2=nil,arg3=nil)
   if(arg2.nil?)
      if(arg0.is_a?(RSignal) and arg1.is_a?(String))
         Qt2::connect(arg0,self,arg1)
      elsif(arg0.is_a?(RSignal) and arg1.is_a?(RSignal))
         Qt2::connect(arg0,arg1)
      end
   elsif
      if(arg3.nil?)
         if(arg0.is_a?(RSignal) and arg1.is_a?(QObject))
           Qt2::connect(arg0,arg1,arg2)
         elsif(arg2.is_a?(RSignal) and arg0.is_a?(QObject))
           Qt2::connect(arg0,arg1,arg2)
         else
           Qt2::connect(arg0,arg1,self,arg2)
         end
      else
         Qt2::connect(arg0,arg1,arg2,arg3)
      end
   end
end
end

#Modify initializaion of QObject. Is it convenient?
#class QObject
#def initialize w=nil, name="", wf=0 , a0=nil, a1=nil, a2=nil, a3=nil, a4=nil, a5=nil, a6=nil, a7=nil, a8=nil, a9=nil, a10=nil, a11=nil, a12=nil, a13=nil, a14=nil
#   catchEvent
#end
#end


#Modifier connect function
def connect(arg1, arg2, arg3=nil, arg4=nil)
   if ( arg1.is_a?(RSignal))
      if (arg2.is_a?(RSignal))
         r = RObject::connect_rsignalrsignal(arg1,arg2)
         return r
      elsif ((arg2.is_a?(QObject)) and (arg3.is_a?(String)))
         if((arg3[0].chr == "1") or (arg3[0].chr == "2")) #Qt2 Signal or Slot
            r = QRObject::connect_rsignal(arg1,arg2,arg3)
            return r
         elsif (arg2.respond_to?(arg3))
            r = QRObject::connect_rsignalslot(arg1,arg2,arg3)
            return r
         else
            fail "3rd argument error in connect(#{arg3})"
            return
         end
      else
         fail "2nd argument error in connect(#{arg2})"
         return
      end
   elsif (((arg1.is_a?(QObject)) or (arg1.is_a?(QMenuBar)))and (arg2.is_a?(String)))
      if (arg2[0].chr == "2") #Qt2 Signal
         if ((arg3.is_a?(Object)) and (arg4.is_a?(String)))
            if((arg4[0].chr == "1") or (arg4[0].chr == "2")) #Qt2 Signal or Slot
               QObject::connect(arg1,arg2,arg3,arg4)
               return
            elsif (not (arg4.is_a?(String)))
               fail "4th argument is not a String in connect(#{arg4})"
               return
            elsif (arg3.respond_to?(arg4))
               r = QRObject::connect_rslot(arg1,arg2,arg3,arg4)
               return r
            elsif (arg3.respond_to?(arg4.sub!('\(.*\)',''))) #Delete terminal (xxx)
               r = QRObject::connect_rslot(arg1,arg2,arg3,arg4)
               return r
            else
               fail "4th argument is not a String or not defined in connect(#{arg4})"
               return
            end
         elsif (arg3.is_a?(RSignal))
            r = QRObject::connect_qsignalrsignal(arg1,arg2,arg3)
            return r
         else
            fail "3th argument error in connect(#{arg3})"
            return
         end
      else
         fail "2nd argument error in connect(#{arg2})"
         return
      end
   else
      fail "1st argument error in connect(#{arg1.type})"
      return
   end
end

#Modifier of connectItem function.
#Override connectItem  in QAccel class
class QAccel
alias qt2_connectItem connectItem
def connectItem( id , receiver, method = nil)
   if (not (id.is_a?(Fixnum)))
      fail "1st argument is not a FIXNUM in connectItem"
      return
   elsif (receiver.is_a?(RSignal))
      return QRObject::connectItem_rsignal(self,id,receiver,method)
   elsif (not (receiver.is_a?(QObject)))
      fail "2nd argument is not a QObject in connectItem"
      return
   elsif (not (method.is_a?(String)))
      fail "3rd argument is not a String in connectItem"
      return
   elsif ((method[0].chr == "1") or (method[0].chr == "2")) # Qt2 signal or slot
      return self.qt2_connectItem(id,receiver,method)
   else
      return QRObject::connectItem_rslot(self,id,receiver,method)
   end
return false
end
end


#For Color Definition
# Use X11 rgb.txt in /usr/X11R6/lib/X11 dir and
# return QColor object
# It is replacement of QColor(const char*) constructor.

ColorDic = Hash.new([0,0,0])
def QColor cname
   dat = findColor cname
   QColor.new(dat[0],dat[1],dat[2],QColor::Rgb)
end

#Find color named s from ColorDic or rgb.txt in X11 lib
#
# Use ColorDic = Hash.new([0,0,0])

def findColor s
   rgbfile = nil
   if (ColorDic.has_key?(s))
      return ColorDic[s]
   end
   if(File::exist?("/usr/X11R6/lib/X11/rgb.txt"))   #XFree86 in Linux
      rgbfile = "/usr/X11R6/lib/X11/rgb.txt"
   elsif(File::exist?("/usr/openwin/lib/rgb.txt")) #Solaris 2.x?
      rgbfile = "/usr/openwin/lib/rgb.txt"
   else
      #p " Now, let's look for rgb.txt in $LOAD_PATH"
	  find = false
	  $LOAD_PATH.each{|path|
	  if(find == false)
	print "seeking #{path}/rgb.txt\n"
	    if(File::exist?("#{path}/rgb.txt"))
		  rgbfile = "#{path}/rgb.txt"
		  find = true
		end
	  end
	  }
   end

   if(rgbfile.nil?)
      print "There is no rgb.txt in /usr/X11R6/lib/X11 or /usr/openwin/lib dir.\n See qt2.rb\n"
      return [0,0,0]
   end
   rgb = File::open(rgbfile,"r")
   rgb.each do |line|
       dat = line.split
       if(dat[0][0] == "!")
             #skip
       else
          data3 = dat[0..2]
          data3 = data3.collect{|i| i.to_i}
          if (dat.size == 4)
             name = dat[3]
          else
             datname = dat[3..(dat.size - 1)]
             name = datname.join(" ")
          end
          ColorDic.store(name, data3)
       end
   end
   rgb.close
   return ColorDic[s]
end

def listColor
   if (ColorDic.size == 0)
      QColor("black")  #read rbg.txt
   end
   ColorDic.keys
end
#For translate QString to String.
# This is move to qt2.qarray.i and translated to qt2_wrap.cc 000315 by horie
#class QString
#def to_str
#   qstr2str(self)
#end
# QString::null defined in qstring.i causes seg. fault? Define here.
def QString.null
   QString.new
end

#For delete function for QObject
def delete obj
    deleteQObj( obj )
    nil
end

end
#For initialization of QRObject
#QRObject_instance = QRObject.new
#Dont make another instance
#def QRObject.new
# print "Do not make new instance of QRObject.\n"
#end
QRObject::QRObj_init()

