mirror of
				https://gitlab.com/comunic/comunicmessages
				synced 2025-11-04 12:14:05 +00:00 
			
		
		
		
	Can send image to conversations
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
#include <QMessageBox>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <QTimer>
 | 
			
		||||
#include <QFileDialog>
 | 
			
		||||
 | 
			
		||||
#include "conversationwidget.h"
 | 
			
		||||
#include "ui_conversationwidget.h"
 | 
			
		||||
@@ -40,6 +41,31 @@ ConversationWidget::~ConversationWidget()
 | 
			
		||||
    delete mTimer;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConversationWidget::setMessageFormImage()
 | 
			
		||||
{
 | 
			
		||||
    //Check if an image has already been selected by the user
 | 
			
		||||
    if(hasUserSelectedImageToSend()){
 | 
			
		||||
 | 
			
		||||
        //Ask user confirmation
 | 
			
		||||
        if(QMessageBox::question(
 | 
			
		||||
                    this,
 | 
			
		||||
                    tr("Unselect image"),
 | 
			
		||||
                    tr("Are you sure to remove currently selected image from message ?")
 | 
			
		||||
            ) != QMessageBox::Yes)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        mPathToCurrentImageInForm = "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Pick an image
 | 
			
		||||
    else
 | 
			
		||||
        mPathToCurrentImageInForm = QFileDialog::getOpenFileName(this,
 | 
			
		||||
              tr("Choose image to include in the message"), "", tr("Image Files (*.png *.jpg *.jpeg, *.gif)"));
 | 
			
		||||
 | 
			
		||||
    //Check if we have an image selected
 | 
			
		||||
    refreshPickImageButton();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConversationWidget::sendMessage()
 | 
			
		||||
{
 | 
			
		||||
    if(isSendMessageFormLocked()){
 | 
			
		||||
@@ -50,7 +76,7 @@ void ConversationWidget::sendMessage()
 | 
			
		||||
    QString content = ui->messageContentInput->text();
 | 
			
		||||
 | 
			
		||||
    //Check message length
 | 
			
		||||
    if(content.length() < CONVERSATION_MESSAGE_MIN_LENGTH){
 | 
			
		||||
    if(content.length() < CONVERSATION_MESSAGE_MIN_LENGTH && !hasUserSelectedImageToSend()){
 | 
			
		||||
        QMessageBox::warning(this, tr("Invalid message!"), tr("Specified message is too short!"));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -63,6 +89,10 @@ void ConversationWidget::sendMessage()
 | 
			
		||||
    newMessage.setIDConversation(mConversation.iD());
 | 
			
		||||
    newMessage.setMessage(content);
 | 
			
		||||
 | 
			
		||||
    //Include user image (if any)
 | 
			
		||||
    if(hasUserSelectedImageToSend())
 | 
			
		||||
        newMessage.setImagePath(mPathToCurrentImageInForm);
 | 
			
		||||
 | 
			
		||||
    //Request the message to be sent
 | 
			
		||||
    mConversationHelper->sendMessage(newMessage);
 | 
			
		||||
}
 | 
			
		||||
@@ -135,6 +165,7 @@ void ConversationWidget::setSendMessageFormLocked(bool lock)
 | 
			
		||||
{
 | 
			
		||||
    ui->sendMessageButton->setEnabled(!lock);
 | 
			
		||||
    ui->messageContentInput->setEnabled(!lock);
 | 
			
		||||
    ui->addImageButton->setEnabled(!lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ConversationWidget::isSendMessageFormLocked()
 | 
			
		||||
@@ -145,4 +176,21 @@ bool ConversationWidget::isSendMessageFormLocked()
 | 
			
		||||
void ConversationWidget::resetSendMessageForm()
 | 
			
		||||
{
 | 
			
		||||
    ui->messageContentInput->setText("");
 | 
			
		||||
    mPathToCurrentImageInForm = "";
 | 
			
		||||
    refreshPickImageButton();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConversationWidget::refreshPickImageButton()
 | 
			
		||||
{
 | 
			
		||||
    ui->addImageButton->setFlat(hasUserSelectedImageToSend());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConversationWidget::on_addImageButton_clicked()
 | 
			
		||||
{
 | 
			
		||||
    setMessageFormImage();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ConversationWidget::hasUserSelectedImageToSend()
 | 
			
		||||
{
 | 
			
		||||
    return !mPathToCurrentImageInForm.isEmpty();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -32,13 +32,17 @@ public:
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Ask the user to choose an image to send with the form
 | 
			
		||||
     */
 | 
			
		||||
    void setMessageFormImage();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Send the message entered by the user in the form
 | 
			
		||||
     */
 | 
			
		||||
    void sendMessage();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
private slots:
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -67,8 +71,18 @@ private slots:
 | 
			
		||||
 | 
			
		||||
    void on_messageContentInput_returnPressed();
 | 
			
		||||
 | 
			
		||||
    void on_addImageButton_clicked();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check out whether the user has selected an image to include
 | 
			
		||||
     * to the next message he will send through the form
 | 
			
		||||
     *
 | 
			
		||||
     * @return TRUE if the user has selected an image / FALSE else
 | 
			
		||||
     */
 | 
			
		||||
    bool hasUserSelectedImageToSend();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Methods to get and set send message form
 | 
			
		||||
     * lock state
 | 
			
		||||
@@ -76,9 +90,11 @@ private:
 | 
			
		||||
    void setSendMessageFormLocked(bool lock);
 | 
			
		||||
    bool isSendMessageFormLocked();
 | 
			
		||||
    void resetSendMessageForm();
 | 
			
		||||
    void refreshPickImageButton();
 | 
			
		||||
 | 
			
		||||
    //Private fields
 | 
			
		||||
    Ui::ConversationWidget *ui;
 | 
			
		||||
    QString mPathToCurrentImageInForm;
 | 
			
		||||
    QTimer *mTimer;
 | 
			
		||||
    ConversationHelper *mConversationHelper;
 | 
			
		||||
    Conversation mConversation;
 | 
			
		||||
 
 | 
			
		||||
@@ -54,6 +54,20 @@
 | 
			
		||||
       </property>
 | 
			
		||||
      </widget>
 | 
			
		||||
     </item>
 | 
			
		||||
     <item>
 | 
			
		||||
      <widget class="QPushButton" name="addImageButton">
 | 
			
		||||
       <property name="text">
 | 
			
		||||
        <string/>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="icon">
 | 
			
		||||
        <iconset resource="../res/ressources.qrc">
 | 
			
		||||
         <normaloff>:/baseline_insert_photo_black_48dp.png</normaloff>:/baseline_insert_photo_black_48dp.png</iconset>
 | 
			
		||||
       </property>
 | 
			
		||||
       <property name="flat">
 | 
			
		||||
        <bool>false</bool>
 | 
			
		||||
       </property>
 | 
			
		||||
      </widget>
 | 
			
		||||
     </item>
 | 
			
		||||
     <item>
 | 
			
		||||
      <widget class="QPushButton" name="sendMessageButton">
 | 
			
		||||
       <property name="text">
 | 
			
		||||
@@ -65,6 +79,8 @@
 | 
			
		||||
   </item>
 | 
			
		||||
  </layout>
 | 
			
		||||
 </widget>
 | 
			
		||||
 <resources/>
 | 
			
		||||
 <resources>
 | 
			
		||||
  <include location="../res/ressources.qrc"/>
 | 
			
		||||
 </resources>
 | 
			
		||||
 <connections/>
 | 
			
		||||
</ui>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user