Commits

Abdelrahman Ghanem committed 3b8aaf7

Update gtk-qt ui, some modifications..

Comments (0)

Files changed (3)

 require 'lib/sum'
 
 $KCODE="u"
-$VERSION="0.1.4-beta"
+$VERSION="0.1.7-beta"
 $ICON=Gdk::Pixbuf.new("icons/calc.png")
 
 class SumWindow < Gtk::Window
   def initialize
     super Gtk::Window::TOPLEVEL
     self.title="RGSum"
-    self.set_default_size(400,400)
+    self.set_default_size(400, 300)
     self.icon_list=[$ICON]
     self.border_width=5
     @sumlib=Sum.new
     @action_paste=Gtk::Action.new('','Paste',nil,Gtk::Stock::PASTE)
     @copy_sep=Gtk::SeparatorMenuItem.new
     @action_save=Gtk::Action.new('','Save',nil,Gtk::Stock::SAVE)
+    @save = @action_save.create_menu_item
     editmenu.append(@action_sum.create_menu_item)
     editmenu.append(@action_stop.create_menu_item)
     editmenu.append(@sum_sep)
     
     #tool bar
     toolbar=Gtk::Toolbar.new
-    @action_open=Gtk::Action.new('','Open',nil,Gtk::Stock::OPEN)
     toolbar.insert(0,@action_open.create_tool_item)
     toolbar.insert(1,@action_sum.create_tool_item)
     toolbar.insert(2,@action_stop.create_tool_item)
     #compare with
     @lblcompare=Gtk::Label.new("Compare with:")
     @entrycompare=Gtk::Entry.new
-    @btncompare=Gtk::Button.new("_Compare")
-    @hboxcompare=Gtk::HBox.new(false,3)
+    
+    @error = Gtk::Stock::DIALOG_ERROR #'icons/error.png'
+    @warning = Gtk::Stock::DIALOG_WARNING #'icons/warning.png'
+    @ok = Gtk::Stock::APPLY #'icons/ok.png'
+    
+    @imagecompare = Gtk::Image.new
+    @imagecompare.stock = @warning
+    #@btncompare=Gtk::Button.new("_Compare")
+    @hboxcompare=Gtk::HBox.new(false, 3)
     @hboxcompare.pack_start(@lblcompare,false,false,1)
     @hboxcompare.pack_start(@entrycompare,true,true,1)
-    @hboxcompare.pack_start(@btncompare,false,false,1)
+    @hboxcompare.pack_start(@imagecompare,false,false,1)
     #progress bar
+    @hboxpb = Gtk::HBox.new(false, 3)
+    @btnshowdetails = Gtk::Button.new
+    @btnshowdetails.image = Gtk::Image.new
+    @btnshowdetails.image.stock = Gtk::Stock::GO_DOWN
+    
+    @btnhidedetails = Gtk::Button.new
+    @btnhidedetails.image = Gtk::Image.new
+    @btnhidedetails.image.stock = Gtk::Stock::GO_UP
+    
     @pb=Gtk::ProgressBar.new
-    #main vbox
+    @hboxpb.pack_start(@btnshowdetails,false,false,1)
+    @hboxpb.pack_start(@btnhidedetails,false,false,1)
+    @hboxpb.pack_start(@pb,true,true,1)
+    
+    #details widget
+    #model
+    @iterators = []
+    @list_store = Gtk::ListStore.new(String, String)
+    ['Filename:', 'Size:',
+     'Reading speed:', 'Reading:',
+     'Elapsed:', 'Estimated:'].zip(['']*6).each do |label, value|
+      iter = @list_store.append
+      iter[0] = label
+      iter[1] = value
+      @iterators << iter
+    end
+    #tree view and scroll window
+    @treedetails = Gtk::TreeView.new(@list_store)
+    @scrollwindow = Gtk::ScrolledWindow.new
+    @scrollwindow.shadow_type = Gtk::SHADOW_OUT
+    @scrollwindow.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC)
+    @scrollwindow.add_with_viewport(@treedetails)
+    
+    @renderer = Gtk::CellRendererText.new
+    labels = Gtk::TreeViewColumn.new('', @renderer, :text=>0)
+    details = Gtk::TreeViewColumn.new('Details', @renderer, :text=>1)
+    
+    @treedetails.append_column(labels)
+    @treedetails.append_column(details)
+
+    #status bar
+    @statusbar = Gtk::Statusbar.new
+    
+    #main vbox, and  vertical paned
+    @vbox = Gtk::VBox.new
+    @vpaned = Gtk::VPaned.new
     @mainvbox=Gtk::VBox.new(false,7)
     @mainvbox.pack_start(menubar,false,false,0)
     @mainvbox.pack_start(toolbar,false,false,0)
     
-    @mainvbox.pack_start(@hboxfile,false,false,1)    
-    @mainvbox.pack_start(@chbox,false,false,1)    
-    @mainvbox.pack_start(@hboxre,true,true,1)
-    @mainvbox.pack_start(@hboxcompare,false,false,1) 
-    @mainvbox.pack_start(@pb,false,false,1)
+    @vbox.pack_start(@hboxfile,false,false,1)    
+    @vbox.pack_start(@chbox,false,false,1)    
+    @vbox.pack_start(@hboxre,true,true,1)
+    @vbox.pack_start(@hboxcompare,false,false,1) 
+    @vbox.pack_start(@hboxpb,false,false,1)
+    @mainvbox.pack_start(@vpaned,true,true,1)
+    @vpaned.add1(@vbox)
+    @vpaned.add2(@scrollwindow)
+    @mainvbox.pack_start(@statusbar,false,false,1)
     
     add(@mainvbox)
+    
     #connect
-    signal_connect("destroy") {
-      do_stop
-      Gtk.main_quit}
-    
-    @action_quit.signal_connect("activate") {
-      do_stop
-      Gtk.main_quit}
+    signal_connect("destroy"){quit}
+    signal_connect("delete-event"){quit}
+    @action_quit.signal_connect("activate") {quit}
     @action_sum.signal_connect("activate") {do_sum}
     @action_stop.signal_connect("activate") {do_stop}
     @action_copy.signal_connect("activate") {cb_copy}
     @action_paste.signal_connect("activate") {cb_paste}
     @action_about.signal_connect("activate") {show_about}
-    
     @action_open.signal_connect("activate") {browse}
     @action_save.signal_connect("activate") {save}
+    @btnshowdetails.signal_connect("clicked"){show_details}
+    @btnhidedetails.signal_connect("clicked"){hide_details}
+    @entrycompare.signal_connect("changed") {do_compare}
+    enable_comp(true)
     
-    @btncompare.signal_connect("clicked") {do_compare}
-    enable_comp(true)
+    @statusbar.push(1, 'Ready')
+  end
+
+  def quit
+    do_stop
+    Gtk.main_quit
+  end
+  
+  def show_details
+    @btnshowdetails.hide
+    @btnhidedetails.show
+    @scrollwindow.show
+  end
+  
+  def hide_details
+    @btnshowdetails.show
+    @btnhidedetails.hide
+    @scrollwindow.hide
   end
   
   def hide_pb
-    @pb.hide
+    @hboxpb.hide
+    @btnhidedetails.hide
+    @scrollwindow.hide
+  end
+  
+  def show_pb
+    @hboxpb.show
+    @btnshowdetails.show
   end
   
   def enable_comp(bool)
       @action_open.sensitive=true
       @action_stop.sensitive=false
       @action_save.sensitive=true
-      @btncompare.sensitive=true
+      #@btncompare.sensitive=true
     else
       @action_sum.sensitive=false
       @action_copy.sensitive=false
       @action_open.sensitive=false
       @action_stop.sensitive=true
       @action_save.sensitive=false
-      @btncompare.sensitive=false
+      #@btncompare.sensitive=false
     end
   end
   
   def do_sum
     return if @entryfile.text.empty?
     enable_comp(false)
-    @pb.show
+    show_pb
     @txtrebuf.text="Now calculating......."
     @sumlib.algo=@combotype.active_text
     @sumlib.filename=@entryfile.text
     size=@sumlib.size.to_f
-    stime=Time.now
+    @iterators[0][1] = File.basename(@sumlib.filename)
+    @iterators[1][1] = @sumlib.to_size(size*100)
+    stime = Time.now
     @sumlib.sum do |i|
       @pb.fraction=i/size
       @pb.text="#{(@pb.fraction*100).to_s.split('.')[0]}%"
+       @elapsed_time = @sumlib.to_time(@sumlib.elapsed)
+      @speed = "#{@sumlib.to_size(@sumlib.read_rate)}/sec"
+      @estimated_time = @sumlib.to_time(@sumlib.estimated)
+      @iterators[2][1] = @speed
+      @iterators[3][1] = @sumlib.to_size(i*100)
+      @iterators[4][1] = @elapsed_time
+      @iterators[5][1] = @estimated_time
       while Gtk.events_pending?
         Gtk.main_iteration
       end
     end
     if !@sumlib.stopped?
-      msg("Elapsed:\n"+@sumlib.to_time(Time.now-stime),'i')
+      @statusbar.push(1, "Done | Elapsed: #{@elapsed_time}")
     end
+    puts @sumlib.to_time(Time.now-stime)
     @txtrebuf.text=@sumlib.result
-    @pb.hide
+    hide_pb
     enable_comp(true)
   end
   
   end
     
   def do_compare
-    return if @txtrebuf.text.empty? or @entrycompare.text.empty?
+    if @txtrebuf.text.empty? or @entrycompare.text.empty?
+      @imagecompare.stock = @warning
+      return
+    end
     if @txtrebuf.text==@entrycompare.text
-      msg("They are the same!","i")
+      @imagecompare.stock = @ok
     else
-      msg("They are not the same!","e")
+      @imagecompare.stock = @error
     end
   end
   
   def show_about
     aboutd=Gtk::AboutDialog.new
-    aboutd.name="RGSum"
-    aboutd.version="0.1.4"
+    aboutd.program_name="RGSum"
+    aboutd.version="0.1.7"
     aboutd.logo=$ICON
     aboutd.copyright="(c) 2009-2010 Abdelrahman Ghanem"
     aboutd.comments="Calculates [md5,sha1,sha224,sha256,sha380,sha512]"
 
 if $0==__FILE__
   swin=SumWindow.new
-  swin.show_all
+  swin.show_all #move it to initailize()?
   swin.hide_pb
   Gtk.main
 end
   def initialize(parent=nil)
     super(parent)
     self.windowTitle="RQSum"
-    self.resize(400, 400)
+    self.resize(400, 300)
     self.windowIcon=icon("calc")
     #status bar
     statusBar.showMessage("Ready..")
     @pb=Qt::ProgressBar.new
     @pb.hide
     
-    #details table
-    @tabledetails = Qt::TableWidget.new(6, 1)
+    #details table, using labels instead?
+    @tabledetails = Qt::TableWidget.new(6, 1) #6 rows, 1 column
     @tabledetails.horizontalHeader.setResizeMode(Qt::HeaderView::Stretch)
+    @tabledetails.verticalHeader.setResizeMode(Qt::HeaderView::Stretch)
     @tabledetails.verticalHeader.defaultSectionSize = 20
     @tabledetails.verticalHeaderLabels = ['Filename:', 'Size:',
                                           'Reading speed:', 'Reading:',
     @hboxpb.addWidget(@btnshowdetails)
     @hboxpb.addWidget(@btnhidedetails)
     @hboxpb.addWidget(@pb)       
-    #central widget
-    @cwidget=Qt::Widget.new
-    self.centralWidget=@cwidget
+    #central widget => splitter, upper widget
+    @splitter = Qt::Splitter.new
+    @splitter.orientation = Qt::Vertical
+    @widget=Qt::Widget.new
+    self.centralWidget=@splitter
     #main
     @vboxmain=Qt::VBoxLayout.new
     @vboxmain.addLayout(@hboxfile)
     @vboxmain.addLayout(@hboxre)
     @vboxmain.addLayout(@hboxcompare)
     @vboxmain.addLayout(@hboxpb)
-    @vboxmain.addWidget(@tabledetails)
+    @widget.layout=@vboxmain
     
-    @cwidget.layout=@vboxmain
+    @splitter.addWidget(@widget)
+    @splitter.addWidget(@tabledetails)
     #connect
     connect(@action_sum,SIGNAL('triggered()'),self,SLOT('sum()'))
     connect(@action_stop,SIGNAL('triggered()'),self,SLOT('stop()'))
     return if filename.empty?
     enable(false)
     show_pb
-    @txtre.setPlainText("Now calculating.......")
     @sumlib.algo=@combotype.currentText
     @sumlib.filename=filename
-    @pb.maximum=@sumlib.size/100
+    @pb.maximum=@sumlib.size
     @pb.format="%p%"
     @txtre.text = "<b>Now</b> calculating......."
     @tabledetails.item(0, 0).text = File.basename(filename)
     @tabledetails.item(1, 0).text = @sumlib.to_size(@sumlib.size*100)
     @sumlib.sum do |i|
       Qt::Application.processEvents
-      @pb.value=i/100
+      @pb.value=i
       @elapsed_time = @sumlib.to_time(@sumlib.elapsed)
       @speed = "#{@sumlib.to_size(@sumlib.read_rate)}/sec"
       @estimated_time = @sumlib.to_time(@sumlib.estimated)
   def about
     Qt::MessageBox.about(self,"About RQSum",
                          "<b>RQSum</b> <br/>Version: 0.1.7<br/>
-                        
                           <br>License: GPL V2 or Later (See COPYING)</br><br></r>
                           <br>Calculates [md5,sha1,sha224,sha256,sha380,sha512]</br><br></br>
                           <br>Website:</br> <a href='http://www.programming-fr34ks.net'>www.programming-fr34ks.net</a>
   app.connect(app,SIGNAL('lastWindowClosed()'),w,SLOT('quit()'))
   w.show
   app.exec
-end
+end
 require 'openssl'
 
 class Sum
+  attr_reader :filename
   
   def filename=(path)
     @filename=path
   
   def size
     #big files? then / 100!
+    #don't forget 100 as a factor while
+    #manipulating with file size
     File.size(@filename)/100
   end
   
   end
   
   def to_size(size_b=length)
+    #better method?
     return if size_b.nil?
     if size_b < 1024
       return "#{size_b} B"
   end
   #thanks to ruby progress bar :)
   def estimated
-    #elapsed * size / @pos.to_f - elapsed #ruby progress bar
-    # or mine: 1/btyes/sec/bytes => sec
-    1/(read_rate / (size - @pos))*100
+    #elapsed * size / @pos.to_f - elapsed #ruby progress bar method
+    # or: 1 / btyes / sec / bytes => sec
+    #1/(read_rate / (size - @pos))*100
+    # or: bytes / bytes /sec => sec
+    (size - @pos)*100 / read_rate
   end
   
   def read_rate
   end
   
 end   
+
+if $0 == __FILE__
+  if ARGV.length < 1
+    $stderr.puts "Argument error, Usage: #{File.basename($0)} [file]"
+  else
+    sum = Sum.new
+    filename = ARGV[0]
+    sum.filename = filename
+    sum.algo = 'md5'
+    puts "File not found" unless File.exists?(filename)
+    sum.sum
+    print sum.result, "\s\s", filename, "\n"
+  end
+end