Commits

Abdelrahman Ghanem committed 42dead1

Add some new features, estimated time and more..

Comments (0)

Files changed (3)

       end
     end
     if !@sumlib.stopped?
-      msg(@sumlib.elapsed(Time.now-stime),'i')
+      msg("Elapsed:\n"+@sumlib.to_time(Time.now-stime),'i')
     end
     @txtrebuf.text=@sumlib.result
     @pb.hide
   
   def show_about
     aboutd=Gtk::AboutDialog.new
-    aboutd.program_name="RGSum"
+    aboutd.name="RGSum"
     aboutd.version="0.1.4"
     aboutd.logo=$ICON
     aboutd.copyright="(c) 2009-2010 Abdelrahman Ghanem"
 #!/usr/bin/ruby
+# coding: utf-8
 # Qt Gui
 # Author: Abdelrahman ghanem <abom.jdev@gmail.com>
 #
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
-
 require 'Qt4'
 require 'lib/sum'
 
 $KCODE="u"
-$VERSION="0.1.4-beta"
+$VERSION="0.1.7-beta"
 
 class MainWindow < Qt::MainWindow
+  
   slots 'browse()','sum()',
         'stop()','paste()',
-	'copy()','compare()',
-	'quit()','about()',
-	'save()'
+        'copy()','compare(QString)',
+        'quit()','about()',
+        'save()',:on_btnshowdetails_clicked,
+        :on_btnhidedetails_clicked
   
   def icon(name)
     Qt::Icon.new("icons/#{name}.png")
   def initialize(parent=nil)
     super(parent)
     self.windowTitle="RQSum"
-    self.resize(400,300)
+    self.resize(400, 400)
     self.windowIcon=icon("calc")
+    #status bar
+    statusBar.showMessage("Ready..")
+
     @sumlib=Sum.new
     #clipboard
     @cb=Qt::Application.clipboard
     #acions:
     @action_open=Qt::Action.new(icon("open"),"&Open",self)
+    @action_open.statusTip="Select a file.."
     @action_quit=Qt::Action.new(icon("quit"),"&Quit",self)
+    @action_quit.statusTip="Quit"
     @action_sum=Qt::Action.new(icon("calc"),"&Sum",self)
+    @action_sum.statusTip="Sum"
     @action_stop=Qt::Action.new(icon("pstop"),"&Stop",self)
+    @action_stop.statusTip="Stop current process"
     @action_copy=Qt::Action.new(icon("copy"),"&Copy",self)
+    @action_copy.statusTip="Copy result"
     @action_paste=Qt::Action.new(icon("paste"),"&Paste",self)
+    @action_paste.statusTip="Paste from clipboard"
     @action_about=Qt::Action.new(icon("about"),"&About",self)
+    @action_about.statusTip="Show about message"
     @action_save=Qt::Action.new(icon("save"),"&Save",self)
+    @action_save.statusTip="Save result to file"
     #menubar:
     @filemenu=Qt::Menu.new("&File")
     @filemenu.addAction(@action_open)
     @txtre=Qt::TextEdit.new
     @txtre.readOnly=true
     @txtre.wordWrapMode=4
-    @txtre.sizePolicy=Qt::SizePolicy::Expanding
     
     @hboxre=Qt::HBoxLayout.new
     @hboxre.addWidget(@lblre)
     @lblcompare=Qt::Label.new("Compare with:")
     @txtcompare=Qt::LineEdit.new
     @txtcompare.readOnly=false
-    @btncompare=Qt::PushButton.new(icon("compare"),"&Compare")
+    #@btncompare=Qt::PushButton.new(icon("compare"),"&Compare")
+    #@btncompare.statusTip="Compare inserted hash with result"
+    @error = Qt::Pixmap.new('icons/error.png')
+    @warning = Qt::Pixmap.new('icons/warning.png')
+    @ok = Qt::Pixmap.new('icons/ok.png')
+    
+    @lblcompare_status = Qt::Label.new
+    @lblcompare_status.pixmap = @warning
     
     @hboxcompare=Qt::HBoxLayout.new
     @hboxcompare.addWidget(@lblcompare)
     @hboxcompare.addWidget(@txtcompare)
-    @hboxcompare.addWidget(@btncompare)
+    #@hboxcompare.addWidget(@btncompare)
+    @hboxcompare.addWidget(@lblcompare_status)
+    
     #progress bar
     @pb=Qt::ProgressBar.new
     @pb.hide
+    
+    #details table
+    @tabledetails = Qt::TableWidget.new(6, 1)
+    @tabledetails.horizontalHeader.setResizeMode(Qt::HeaderView::Stretch)
+    @tabledetails.verticalHeader.defaultSectionSize = 20
+    @tabledetails.verticalHeaderLabels = ['Filename:', 'Size:',
+                                          'Reading speed:', 'Reading:',
+                                          'Elapsed:', 'Estimated:']
+    @tabledetails.horizontalHeaderLabels = ['Details']
+    @tabledetails.selectionBehavior = Qt::AbstractItemView::SelectRows
+    
+    #intialize table with items at column 0
+    for i in 0...6
+      @tabledetails.setItem(i, 0, Qt::TableWidgetItem.new(''))
+    end
+    
+    @btnshowdetails = Qt::PushButton.new(icon('down'), '')
+    @btnshowdetails.objectName = "btnshowdetails"
+    @btnshowdetails.statusTip="Show details"
+    @btnhidedetails = Qt::PushButton.new(icon('up'), '')
+    @btnhidedetails.objectName = "btnhidedetails"
+    @btnhidedetails.statusTip="Hide details.."
+    @btnhidedetails.hide
+    @btnshowdetails.hide
+    @tabledetails.hide
+    
+    #hbox show/hide and pb
+    @hboxpb = Qt::HBoxLayout.new
+    @hboxpb.addWidget(@btnshowdetails)
+    @hboxpb.addWidget(@btnhidedetails)
+    @hboxpb.addWidget(@pb)       
     #central widget
     @cwidget=Qt::Widget.new
     self.centralWidget=@cwidget
     @vboxmain.addLayout(@hboxtype)
     @vboxmain.addLayout(@hboxre)
     @vboxmain.addLayout(@hboxcompare)
-    @vboxmain.addWidget(@pb)
+    @vboxmain.addLayout(@hboxpb)
+    @vboxmain.addWidget(@tabledetails)
     
     @cwidget.layout=@vboxmain
     #connect
     connect(@action_quit,SIGNAL('triggered()'),self,SLOT('quit()'))
     connect(@action_about,SIGNAL('triggered()'),self,SLOT('about()'))
     connect(@action_save,SIGNAL('triggered()'),self,SLOT('save()'))
-    connect(@btncompare,SIGNAL('clicked()'),self,SLOT('compare()'))
+    #connect(@btncompare,SIGNAL('clicked()'),self,SLOT('compare()'))
+    connect(@txtcompare,SIGNAL('textChanged(QString)'),self,SLOT('compare(QString)'))
     #enable - disable actions
     enable(true)
+    
+    Qt::MetaObject.connectSlotsByName(self)
+  end
+  
+  def on_btnshowdetails_clicked
+    @tabledetails.show
+    @btnhidedetails.show
+    @btnshowdetails.hide
+  end
+  
+  def on_btnhidedetails_clicked
+    @tabledetails.hide
+    @btnhidedetails.hide
+    @btnshowdetails.show
+  end
+  
+  def hide_pb
+    @pb.hide
+    @btnshowdetails.hide
+    @btnhidedetails.hide
+    @tabledetails.hide
+  end
+  
+  def show_pb
+    @pb.show
+    @btnshowdetails.show
   end
   
   def sum
     filename=@txtfile.text
     return if filename.empty?
     enable(false)
-    @pb.show
+    show_pb
     @txtre.setPlainText("Now calculating.......")
     @sumlib.algo=@combotype.currentText
     @sumlib.filename=filename
     @pb.maximum=@sumlib.size/100
     @pb.format="%p%"
-    stime=Time.now
-    @sumlib.sum do |i|
+    @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, read_rate, elapsed, estimated|
       Qt::Application.processEvents
       @pb.value=i/100
+      @elapsed_time = @sumlib.to_time(@sumlib.elapsed)
+      @speed = "#{@sumlib.to_size(@sumlib.read_rate)}/sec"
+      @estimated_time = @sumlib.to_time(@sumlib.estimated)
+      @tabledetails.item(2, 0).text = @speed
+      @tabledetails.item(3, 0).text = @sumlib.to_size(i*100)
+      @tabledetails.item(4, 0).text = @elapsed_time
+      @tabledetails.item(5, 0).text = @estimated_time
     end
-    if !@sumlib.stopped?
-      Qt::MessageBox.information(self,"Elapsed",
-				 @sumlib.elapsed((Time.now-stime)))
+    if @sumlib.stopped?
+      statusBar.showMessage('Stopped')
+    else
+      statusBar.showMessage("Done | Elapsed: #{@elapsed_time}")
     end
     @txtre.setPlainText(@sumlib.result)
-    @pb.hide
+    hide_pb
     enable(true)
   end
   
     @txtfile.text=file if !file.nil?
   end
   
-  def compare
-    return if @txtre.plainText.empty? or @txtcompare.text.empty?
-    if @txtre.plainText==@txtcompare.text
-      Qt::MessageBox.information(self,
-                                 "Compare",
-                                 "They are the same!")
+  def compare(hash)
+    if hash.empty?
+      @lblcompare_status.pixmap = @warning
+      return
+    end
+    if @txtre.toPlainText==hash
+      @lblcompare_status.pixmap = @ok
+      #Qt::MessageBox.information(self,
+      #                           "Compare",
+      #                           "They are the same!")
     else
-      Qt::MessageBox.critical(self,
-                              "Compare",
-                              "They are not the same!")
+      @lblcompare_status.pixmap = @error
+      #Qt::MessageBox.critical(self,
+      #                        "Compare",
+      #                        "They are not the same!")
     end
   end
   
   
   def about
     Qt::MessageBox.about(self,"About RQSum",
-                         "<b>RQSum</b> <br/>Version: 0.1.4<br/>
+                         "<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>
-                          <br>Copyright (c) 2009-2010 Abdelrahman Ghanem</br> <a href='mailto:abom.jdev@gmail.com'>abom.jdev@gmail.com</a>")
+                          <br>Copyright (c) 2009-2011 Abdelrahman Ghanem</br> <a href='mailto:abom.jdev@gmail.com'>abom.jdev@gmail.com</a>")
   end
   
   def enable(value)
       @action_paste.enabled=true
       @action_about.enabled=true
       @action_save.enabled=true
-      @btncompare.enabled=true
+      #@btncompare.enabled=true
     else
       @action_open.enabled=false
       @action_quit.enabled=false
       @action_paste.enabled=false
       @action_about.enabled=false
       @action_save.enabled=false
-      @btncompare.enabled=false
+      #@btncompare.enabled=false
     end
   end
   
   app.connect(app,SIGNAL('lastWindowClosed()'),w,SLOT('quit()'))
   w.show
   app.exec
-end
+end
 
 class Sum
   
-  def initialize
-    @error=nil
-  end
-  
   def filename=(path)
     @filename=path
   end
     @stop
   end
   
-  def elapsed(seconds)
+  def to_time(seconds)
+    #Time.at(seconds)-2hours
     fill=lambda{|i|'0'*(2-i.round.to_s.length)+i.to_s}
     hours , hours_fraction = seconds.divmod(60*60)
     minutes, minutes_fraction = hours_fraction.divmod(60)
     fill[hours]+':'+fill[minutes]+':'+fill[seconds]
   end
   
+  def to_size(size_b=length)
+    return if size_b.nil?
+    if size_b < 1024
+      return "#{size_b} B"
+    elsif size_b > 1024 and size_b < (1024**2)
+      kb=size_b.to_f / 1024
+      return "#{(kb*100).round.to_f/100} KB"
+    elsif size_b > (1024**2) and size_b < (1024**3)
+      mb=size_b.to_f / (1024**2)
+      return "#{(mb*100).round.to_f/100} MB"
+    elsif size_b > (1024**3) and size_b < (1024**4)
+      gb=size_b.to_f / (1024**3)
+      return "#{(gb*100).round.to_f/100} GB"
+      #elsif size_b > (1024**4) HEHEHEHE 'TB' LOL :D
+    end
+  end
+
+  def elapsed
+    Time.now - @start_time
+  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
+  end
+  
+  def read_rate
+    # bytes/seconds
+    (@pos / elapsed.to_f) * 100
+  end
+  
   def sum(bufsize=16192*50)
+    @start_time = Time.now
     begin
       File.open(@filename,'rb') do |ifile|
         until ifile.eof? do
 	  break if @stop
           @alg << ifile.read(bufsize)
-          yield(ifile.pos/100) if block_given?
+	  @pos = ifile.pos/100 
+          yield(@pos) if block_given?
         end
       end
     rescue => e
-      @error=e.message
+      raise e.message
     end
   end
   
     end
   end
   
-  def lasterror
-    @error
-  end
-  
-end   
+end